给定:
模板< typename T& void f(T){} enum {//如果更改为enum Eit it compiles e } int main(){ f(e); // line 10 }我得到:
foo.cpp:在函数'int main()': foo.cpp:10:错误:没有匹配的函数调用'f ; anonymous enum>)'但如果枚举声明给出一个名字,它编译。为什么它不适用于匿名枚举?理想情况下,我想把枚举值 e 提升为 int ,并实例化未命名的类型不能用作模板参数
>C ++ 03在中说14.3.1 [temp.arg.type] / 2
本地类型,没有链接的类型,未命名类型或从这些类型中任何一个复合的类型都不能用作模板类型的模板参数 - 参数。
这个限制在C ++ 0x中解除了,你的程序编译时没有MSVC ++ 2010和gcc 4.5的诊断。 2在C ++ 0x模式下。
Given:
template<typename T> void f( T ) { } enum { // if changed to "enum E" it compiles e }; int main() { f( e ); // line 10 }I get:
foo.cpp: In function ‘int main()’: foo.cpp:10: error: no matching function for call to ‘f(<anonymous enum>)’Yet if the enum declaration is given a name, it compiles. Why doesn't it work for an anonymous enum? Ideally, I'd like it to promote the enum value e to an int and instantiate f(int).
解决方案Unnamed type simply cannot be used as a template argument
C++03 says in 14.3.1[temp.arg.type]/2
A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.
This limitation was lifted in C++0x, and your program compiles with no diagnostics with MSVC++ 2010 and gcc 4.5.2 in C++0x mode.
更多推荐
没有匹配的函数调用< anonymous enum>
发布评论