Local classes : C++03 vs. C++11

与世无争的帅哥 提交于 2019-11-30 11:02:37

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.

the_drow

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!