给定:
template<typename T>
void f( T ) {
}
enum { // if changed to "enum E" it compiles
e
};
int main() {
f( e ); // line 10
}
我得到:
foo.cpp: In function ‘int main()’:
foo.cpp:10: error: no matching function for call to ‘f(<anonymous enum>)’
然而,如果为 enum
声明命名,它就会编译。为什么它不适用于匿名枚举?理想情况下,我希望它将枚举值 e
提升为 int
并实例化 f(int)
。
最佳答案
未命名类型根本不能用作模板参数
C++03 在 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.
此限制在 C++0x 中解除,您的程序在 C++0x 模式下使用 MSVC++ 2010 和 gcc 4.5.2 进行编译时没有诊断。
关于c++ - 没有对 <anonymous enum> 的匹配函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5720359/