Incomplete type is not allowed in a class, but is allowed in a class template

前端 未结 4 1129
野的像风
野的像风 2021-02-07 03:50

The following is invalid code:

struct foo {
    struct bar;
    bar x;        // error: field x has incomplete type
    struct bar{ int value{42}; };
};

int mai         


        
4条回答
  •  离开以前
    2021-02-07 04:09

    I think this example is explicitly allowed by

    17.6.1.2 Member classes of class templates [temp.mem.class]

    1 A member class of a class template may be defined outside the class template definition in which it is declared. [Note: The member class must be defined before its first use that requires an instantiation (17.8.1) For example,

    template struct A {
      class B;
    };
    
    A::B* b1; // OK: requires A to be defined but not A::B
    template class A::B { };
    A::B b2; // OK: requires A::B to be defined
    

    —end note ]

    This should work fine too:

    template 
    struct foo_impl {
        struct bar;
        bar x;        // no problems here
    };
    
    template
    struct foo_impl::bar{ int value{42}; };
    
    using foo = foo_impl<>;
    
    int main()
    {
        return foo{}.x.value;
    }
    

提交回复
热议问题