Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching function call to <anonymous enum>

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).

like image 772
Paul J. Lucas Avatar asked Apr 19 '11 17:04

Paul J. Lucas


2 Answers

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.

like image 65
Cubbi Avatar answered Oct 24 '22 23:10

Cubbi


Ideally, I'd like it to promote the enum value e to an int and instantiate f(int).

f(+e);
like image 34
curiousguy Avatar answered Oct 25 '22 01:10

curiousguy