没有匹配的函数调用<anonymous enum>

Pau*_*cas 10 c++ enums templates

鉴于:

template<typename T>
void f( T ) {
}

enum {    // if changed to "enum E" it compiles
  e
};

int main() {
  f( e ); // line 10
}
Run Code Online (Sandbox Code Playgroud)

我明白了:

foo.cpp: In function ‘int main()’:
foo.cpp:10: error: no matching function for call to ‘f(<anonymous enum>)’
Run Code Online (Sandbox Code Playgroud)

然而,如果enum声明被赋予一个名称,它就会编译.为什么它不能用于匿名枚举?理想情况下,我希望它能够将枚举值e提升为int实例化f(int).

Cub*_*bbi 10

未命名的类型根本不能用作模板参数

C++ 03说道 14.3.1[temp.arg.type]/2

本地类型,没有链接的类型,未命名的类型或从这些类型中的任何类型复合的类型不应该用作模板类型参数的模板参数.

此限制在C++ 0x中解除,并且您的程序在C++ 0x模式下无法使用MSVC++ 2010和gcc 4.5.2进行编译.


cur*_*guy 5

理想情况下,我希望它将枚举值e提升为int并实例化f(int).

f(+e);
Run Code Online (Sandbox Code Playgroud)