功能模板:typename声明

时间:2011-04-20 18:15:30

标签: c++ templates

在GCC上,以下内容给出了一个错误:no type named 'x' in 'struct Type'

在VC ++上,它抱怨p未声明

struct Type
{
   static int const x = 0;
};

template <class T> void Func()
{
   typename T::x * p; // p to be pointer
}

int main()
{
   Func<Type>();
}

3 个答案:

答案 0 :(得分:4)

T::x变为Type::x,这是int,而不是类型。

您告诉编译器T::x使用typename命名一个类型。实例化Func<Type>时,T::x不是类型,因此编译器报告错误。

答案 1 :(得分:0)

由于Type::x不是类型,而是,所以当您编写typename时,您告诉编译器要查找x中名为Type的嵌套类型,但它不能。因此,GCC说no type named 'x' in 'struct Type'比VC ++生成的消息更有用。

答案 2 :(得分:0)

在C ++ 11中,using关键字可用于类型别名

struct Type
{
    using x = static int const;
};
相关问题