为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C++
千万里不及你 2019-10-18 13:46:14
我有在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 ) {}
查看完整描述

3 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

如果没有显式地专门化包含类,就不能专门化成员函数。

但是,您可以做的是向前调用部分专用类型的成员函数:


template<class T, class Tag>

struct helper {

    static void f(T);   

};


template<class T>

struct helper<T, tag1> {

    static void f(T) {}

};


template<class T>

struct C {

    // ...

    template<class Tag>

    void foo(T t) {

        helper<T, Tag>::f(t);

    }

};


查看完整回答
反对 回复 2019-10-18
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

我知道这可能无法满足您的要求,但是我不认为您可能没有在非明确的专业结构中包含专业化知识。


template<>

template<>

inline void C< tag1 >::foo< tag2 >( t_type ) {}


查看完整回答
反对 回复 2019-10-18
  • 3 回答
  • 0 关注
  • 494 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信