2013-07-20 11 views
3

클래스 전문화 안에 불완전한 구조체를 선언하고 나중에 정의하는 데 문제가 있습니다.특수 클래스의 불완전한 구조체 정의

struct Foo { 
    template <bool Y, typename D> 
    struct Bar {}; 

    template <typename D> 
    struct Bar<true, D> { 
     struct Qux; 
    }; 

    template <typename D> 
    struct Bar<true, D>::Qux { int x; }; 
}; 

이 코드는 GCC에서 작동하지만, 그 소리 3.3 실패 : 코드가 (struct Foo없이) 네임 스페이스 범위에 기록

r.cpp:42:26: error: non-friend class member 'Qux' cannot have a qualified name 
    struct Bar<true, D>::Qux { int x; }; 
      ~~~~~~~~~~~~~~^ 

경우, 너무 그 소리에서 작동합니다.

한편, struct Foo이 템플릿으로 바뀌면 다음과 같이 gcc-4.7에서 코드가 깨집니다 (출시되지 않음). gcc-4.7에서 계속 작동합니다.

template <typename X> 
struct Foo { 
    template <bool Y, typename D> 
    struct Bar {}; 

    template <typename D> 
    struct Bar<true, D> { 
     struct Qux; 
    }; 

    template <typename D> 
    struct Bar<true, D>::Qux { int x; }; 
}; 

연타가 실패합니다

r.cpp:43:26: error: template specialization or definition requires a template parameter list corresponding to the nested type 'Bar<true, type-parameter-1-0>' 
    struct Bar<true, D>::Qux { int x; }; 
         ^
r.cpp:43:26: error: non-friend class member 'Qux' cannot have a qualified name 
    struct Bar<true, D>::Qux { int x; }; 
      ~~~~~~~~~~~~~~^ 
2 errors generated. 

GCC-4.9 유사한 오류와 함께 실패합니다 : 당신이 넣어하지만 선택의 여지가없는 것

r.cpp:43:26: error: too few template-parameter-lists 
    struct Bar<true, D>::Qux { int x; }; 
         ^

답변

2

가 보이는 네임 스페이스 범위 (또는 Bar 내부)의 정의. 문단 9/1 (n3337)에 귀하의 코드가 불법이라고 말했습니다 :

If a class-head-name contains a nested-name-specifier, the class-specifier shall refer to a class that was previously declared directly in the class or namespace to which the nested-name-specifier refers, or in an element of the inline namespace set (7.3.1) of that namespace (i.e., not merely inherited or introduced by a using-declaration), and the class-specifier shall appear in a namespace enclosing the previous declaration. In such cases, the nested-name-specifier of the class-head-name of the definition shall not begin with a decltype-specifier.

-3
struct Foo { 
    template <bool Y, typename D> 
    struct Bar {}; 
}; 

template <typename D> 
struct Foo::Bar<true, D> { 
    struct Qux; 
}; 

template <typename D> 
struct Foo::Bar<true, D>::Qux { 
    int x; 
}; 
+1

왜 설명 할 수 있습니까? –

+0

-1 코드 만 있고 설명이없는 대답 은요? – Manu343726