2011-05-10 41 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>' 
    }; 
} 


因爲我已經知道如何解決這種錯誤,我定義專業化類範圍之外,但是我得到了另一個錯誤 - ......沒有模板參數使用:

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'在用戶編寫的代碼中是非法的。 – 2011-05-10 13:08:02

+0

謝謝,這只是當我簡化有問題的代碼時想到的第一件事 – Ryan 2011-05-10 13:29:20

+0

從什麼遷移? – jalf 2011-05-10 13:33:56

回答

3

你需要要麼完全專注的一切,就像這樣:

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和許多其他人不會明白) :

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 2011-05-10 13:51:39

+0

謝謝,弗拉德,我會嘗試解決方法... – Ryan 2011-05-10 13:53:36

+0

是的,這沒有把戲! – Ryan 2011-05-10 14:02:50

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.

當你明確地專注一個成員函數模板而非類成員模板,它應該是罰款;然而,無論是科莫,GCC,也沒有VC++允許以下,這應該是正確的語法:

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,這是有道理的,如果我們在§14.7.3/ 18應用規則成員函數模板以及
  • GCC說invalid explicit specialization before '>' token; enclosing class templates are not explicitly specialized,這又是有道理的,如果我們在§14.7.3/ 18應用規則的成員函數模板以及
  • VC++說error C2768: 'test::Test<A,B,C>::check' : illegal use of explicit template arguments,這不是一個有用的錯誤消息,但線一般從以人

我的猜測是必須有一個缺陷報告提交,也包含成員函數模板的明確專業化,當封閉的類模板也沒有明確專門化;然而,由於第14.7.3/18節的措辭在C++ 03標準和C++ 0x FDIS之間沒有改變(如果DR是針對C++提交的03並被接受)。

+0

+1爲標準的報價 – 2011-05-10 13:52:49

相關問題