为什么此可变参数模板专业化定义无法编译? [英] Why does this variadic template specialization definition not compile?

查看:89
本文介绍了为什么此可变参数模板专业化定义无法编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gcc 4.7.3,出现以下错误

Using gcc 4.7.3, I get the following error


prog.cpp:在函数'int main()'中:prog .cpp:27:63:错误:
'Erase> :: Result'尚未被声明

prog.cpp: In function ‘int main()’: prog.cpp:27:63: error: ‘Erase >::Result’ has not been declared

使用此代码

template <typename... List>
struct TypeList
{
    enum
    {
        Length = sizeof...(List)
    };
};

template <typename ToErase, typename... List>
struct Erase;

template <typename ToErase>
struct Erase<ToErase, TypeList<>>
{
    typedef TypeList<> Result;
};

template <typename ToErase, typename... Head, typename... Tail>
struct Erase<ToErase, TypeList<Head..., ToErase, Tail...>>
{
    typedef TypeList<Head..., Tail...> Result;
};

int main()
{
    static_assert(Erase<double, TypeList<int, double, char>>::Result::Length == 2, 
    "Did not erase double from TypeList<int, double, char>");

    return 0;
}

由于错误消息,我不明白为什么代码无法编译鉴于类似的情况确实可以很好地编译,所以我收到了:

I don't understand why the code doesn't compile given the error message I've received, given that a similar case does compile cleanly:

template <typename ToAppend, typename... List>
struct Append;

template <typename ToAppend, typename... List>
struct Append<ToAppend, TypeList<List...>>
{
    typedef TypeList<List..., ToAppend> Result;
}

template <typename... ToAppend, typename... List>
struct Append<TypeList<ToAppend...>, TypeList<List...>>
{
    typedef TypeList<List..., ToAppend...> Result;
}

标准中是否有关于无法推断元素的报价?

Is there a quote from the standard about not being able to deduce elements in the middle of two parameter packs like I'm trying to do with the first block of code?

推荐答案

§之类的两个参数包的中间? 14.8.2.5(从类型推导模板参数)第5段列出了无法推导模板参数的上下文。相关的是列表中的最后一个:

§ 14.8.2.5 (Deducing template arguments from a type) paragraph 5 lists the contexts in which template arguments cannot be deduced. The relevant one is the last one in the list:


-在参数声明的末尾没有出现的函数参数包-子句。

— A function parameter pack that does not occur at the end of the parameter-declaration-clause.

所以在:

struct Erase<ToErase, TypeList<Head..., ToErase, Tail...>>

Head

相比之下,

struct Append<TypeList<ToAppend...>, TypeList<List...>>

要追加 List 出现在它们各自的参数列表的末尾,因此可以推导出它们。

Both ToAppend and List appear at the end of their respective parameter lists, and hence they can be deduced.

这篇关于为什么此可变参数模板专业化定义无法编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆