用于在模板类中显式专门化模板函数的C ++语法?

我有在VC9(Microsoft Visual C ++ 2008 SP1)中有效的代码,但在GCC 4.2(在Mac上)中无效:


struct tag {};


template< typename T >

struct C

{   

    template< typename Tag >

    void f( T );                 // declaration only


    template<>

    inline void f< tag >( T ) {} // ERROR: explicit specialization in

};                               // non-namespace scope 'structC<T>'

我知道GCC希望我将显式专业移至类之外,但我无法弄清楚语法。有任何想法吗?


// the following is not correct syntax, what is?

template< typename T >

template<>

inline void C< T >::f< tag >( T ) {}


千万里不及你
浏览 498回答 3
3回答

一只名叫tom的猫

如果没有显式地专门化包含类,就不能专门化成员函数。但是,您可以做的是向前调用部分专用类型的成员函数:template<class T, class Tag>struct helper {&nbsp; &nbsp; static void f(T);&nbsp; &nbsp;};template<class T>struct helper<T, tag1> {&nbsp; &nbsp; static void f(T) {}};template<class T>struct C {&nbsp; &nbsp; // ...&nbsp; &nbsp; template<class Tag>&nbsp; &nbsp; void foo(T t) {&nbsp; &nbsp; &nbsp; &nbsp; helper<T, Tag>::f(t);&nbsp; &nbsp; }};

拉莫斯之舞

我知道这可能无法满足您的要求,但是我不认为您可能没有在非明确的专业结构中包含专业化知识。template<>template<>inline void C< tag1 >::foo< tag2 >( t_type ) {}
打开App,查看更多内容
随时随地看视频慕课网APP