函数模板:typename声明
Function Template: typename declaration
在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>();
}
T::x
变为Type::x
,它是int
,而不是类型。
您已经告诉编译器T::x
通过使用typename
来命名类型。实例化Func<Type>
时,T::x
不是类型,因此编译器会报告错误。
由于Type::x
不是类型,而是值。因此,当您编写typename
时,您会告诉编译器在Type
中找到一个名为x
的嵌套型但它做不到。因此GCC说no type named 'x' in 'struct Type'
,这比VC++生成的消息更有帮助。
在C++11中,using关键字可以用于类型别名
struct Type
{
using x = static int const;
};
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 第二个指定的类型在模板typename声明中意味着什么
- C++在继承构造函数中使用具有typename的声明
- "typename qualified-id"引用非类型参数声明中的类型
- 匿名模板typename/class声明
- 正向声明-Typename C++错误
- 函数模板:typename声明