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.
A class declared inside a function becomes local to that function and is called Local Class in C++. A local class name can only be used locally i.e., inside the function and not outside it. The methods of a local class must be defined inside it only. A local class can have static functions but, not static data members.
Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method. This section covers the following topics: Declaring Local Classes. Accessing Members of an Enclosing Class.
C Classes A class consists of an instance type and a class object: An instance type is a struct containing variable members called instance variables and function members called instance methods. A variable of the instance type is called an instance.
You make the contents of a class public by putting methods from it in the 'public' part of the class definition, like this: class MyClass { public: MyClass(); ~MyClass(); int getValue() const; private: void setValue(int i); }; Now everybody may construct and destruct instances of this class, and may even get the value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With