Is there any change in the usage of local class in C++11?
It seems in C++03 local classes cannot be used as template argument (I recall that).
Consider this code,
template<typename T> void f(const T&) {}
//Note : S is a local class defined inside main()
int main() { struct S{}; f(S()); } //I want template argument to be deduced.
But it gives compilation error (C++03 mode), saying (ideone):
prog.cpp:4: error: no matching function for call to ‘f(main()::S)’
However, it compiles fine when compiling it in C++11 mode (ideone), which makes sense to me, otherwise lambda wouldn't work. So I guess that there is at least this change in the usage of local classes. Am I right? What are other changes concerning local classes?
Please quote the relevant text from the Standards (C++03 and C++11 both) so readers can compare themselves, and for future reference.
The differences are visible by comparing §14.3.1/2 in both standards.
C++03
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. [Example:
template <class T> class X { /* ... */ }; void f() { struct S { /* ... */ }; X<S> x3; // error: local type used as template-argument X<S*> x4; // error: pointer to local type used as template-argument }
—end example] [Note: a template type argument may be an incomplete type (3.9). ]
C++0x (n3290)
[ Example:
template <class T> class X { }; template <class T> void f(T t) { } struct { } unnamed_obj; void f() { struct A { }; enum { e1 }; typedef struct { } B; B b; X<A> x1; // OK X<A*> x2; // OK X<B> x3; // OK f(e1); // OK f(unnamed_obj); // OK f(b); // OK }
— end example ] [ Note: A template type argument may be an incomplete type (3.9). — end note ]
C++03 explicitly disallows local classes in template type arguments. C++11 doesn't, and even includes an example of a valid use of such.
From older standard:
(14.3) 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.
It seems to be removed in the C++11 standard.
More restrictions:
(9.8) Declarations in a local class can use only type names, static variables, extern variables and functions, and enumerators from the enclosing scope.
(9.8) A local class shall not have member templates.
(14.5.4) A friend template shall not be declared in a local class.
(9.4.2 ) A local class shall not have static data members.
(9.3) Member functions of a local class (9.8) have no linkage.
According to my own question the limitation is removed and local classes can be used as template arguments.
I see no reference to the new standard though.
来源:https://stackoverflow.com/questions/8203750/local-classes-c03-vs-c11