2011-05-10 4 views
4

나는 다시 한번 당신의 도움이 필요 해요 ... 매개 변수 는 ... 오류

내가했다 오류가 아닌 네임 스페이스 범위에 명시 적 전문화를 발생시킨 다음 코드 :

: ... 템플릿 매개 변수없이 사용 -


이미 오류 이런 종류의 문제를 해결하는 방법을 알고 있기 때문에

namespace __test 
{ 
    template <int A, int B, typename C> class Test 
    { 
     template <int V> void check(C & a) { } 
     template <> void check<0>(C & a) { } //error: explicit specialization in non-namespace scope 'class __test::Test<A, B, C>' 
    }; 
} 
, 그러나 나는 내가 다른 오류를 가지고, 클래스 범위 이외의 전문화를 정의 6,
namespace __test 
{ 
    template <> void Test::check<0>(C & a) { } //error: 'template<int A, int B, class C> class __test::Test' used without template parameters 
} 


나는 아마 바보 해요,하지만 난이 문제의 원인을 이해하지 못하는 내가 도와주세요 ... 그것을 해결하는 방법을 모르겠어요!

+5

'__test'이름은 사용자 작성 코드에서 불법입니다. –

+0

문제가되는 코드를 단순화 할 때 가장 먼저 떠오른 것이 – Ryan

+0

무엇에서 마이그레이션 했습니까? – jalf

답변

3

이 같은 어느 완전히 모든 것을 전문으로합니다 :

namespace __test { 

template <typename C, int V> 
struct TestHelper 
{ 
    static void check (C & a) 
    { 
    } 
}; 

template <typename C> 
struct TestHelper<C, 0> 
{ 
    static void check (C & a) 
    { 
    } 
}; 

template <int A, int B, typename C> 
class Test 
{ 
    template <int V> void check(C & a) 
    { 
     TestHelper<C, V>::check (a); 
    } 
}; 

} 
+0

+1 제안 된 해결 방법 – ildjarn

+0

감사합니다, 블라드, 나는 회피 방법을 시도합니다 ... – Ryan

+0

그래,이 트릭을! – Ryan

4

표준을 읽음으로써, 당신이하고자하는 것은 합법적 인 것처럼 보입니다. §14.7.3/18 인용 :

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well. In such explicit specialization declaration, the keyword template followed by a template-parameter-list shall be provided instead of the template<> preceding the explicit specialization declaration of the member. The types of the template-parameters in the template-parameter-list shall be the same as those specified in the primary template definition.

명시 적으로 클래스 멤버 템플릿보다는 멤버 함수 템플릿을 전문으로하고, 그것은 잘해야한다; 우리가 §14.7.3/18 규칙을 적용하는 경우

namespace test 
{ 
    template<int A, int B, typename C> 
    class Test 
    { 
     template<int V> 
     void check(C& a) { } 
    }; 

    template<int A, int B, typename C> 
    template<> 
    void Test<A, B, C>::check<0>(C& a) { } 
} 
  • 꼬모가 error: a template declaration containing a template parameter list may not be followed by an explicit specialization declaration을 말한다 의미가 있습니다 : 그러나, 어느 쪽도 아니 꼬모는, GCC,도 VC는 올바른 구문해야하는 , 다음을 허용하지 ++ 구성원 함수 템플릿도 마찬가지입니다.
  • GCC는 invalid explicit specialization before '>' token을 말합니다. 우리가 도움이 오류 메시지가없는
  • VC++의 error C2768: 'test::Test<A,B,C>::check' : illegal use of explicit template arguments을 말한다 아니라 멤버 함수 템플릿에 §14.7.3/18 규칙을 적용하지만 일반적으로 다른 사람과 라인에 떨어지면 다시 의미가 enclosing class templates are not explicitly specialized,

추측은 첨부 된 클래스 템플릿이 명시 적으로 특수화되지 않은 경우 멤버 함수 템플릿의 명시 적 특수화를 허용하지 않는 결함 보고서가 있어야한다는 것입니다. 그러나 C++ 03 표준과 C++ 0x FDIS 사이에서 §14.7.3/18에 대한 문구가 변경되지 않았으므로이를 명확하게 말할 수는 없습니다 (DR이 C++에 대해 제기 된 경우 03 및 허용).

namespace __test { 

template <int A, int B, typename C> 
class Test 
{ 
    template <int V> void check(C & a) { } 
}; 

template <> 
template <> 
void Test<1, 2, int>::check<0> (int &) 
{ 
} 

} 

또는 부분적으로 템플릿 템플릿 클래스의 방법 전문하려고 피하기 위해 도우미 구조를 사용 (GCC를하고 많은 사람들이 이해하지 못할) :

+0

표준에서 인용문을 위해 +1 –