Paul J. Lucas
Paul J. Lucas

Reputation: 7123

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

Upvotes: 10

Views: 2922

Answers (3)

curiousguy
curiousguy

Reputation: 8270

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

f(+e);

Upvotes: 5

Cubbi
Cubbi

Reputation: 47418

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.

Upvotes: 10

Mark B
Mark B

Reputation: 96241

You can always explicitly do the promotion to clearly show your intention:

f(static_cast<int>(e));

Upvotes: 1

Related Questions