加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C部分方法专业化

发布时间:2020-12-16 05:55:38 所属栏目:百科 来源:网络整理
导读:模板类方法有部分专业化吗? template class A,class B class C { void foo(); } 它不能像这样专门化: template class A void CA,CObject::foo() {}; 任何帮助? 解决方法 如果你已经有了专门的课程,你可以在专业课上给予不同的foo实现: templatetypename
模板类方法有部分专业化吗?
template <class A,class B>
 class C
 {
  void foo();
 }

它不能像这样专门化:

template <class A> void C<A,CObject>::foo() {};

任何帮助?

解决方法

如果你已经有了专门的课程,你可以在专业课上给予不同的foo实现:
template<typename A,typename B>
class C
{
public:
    void foo() { cout << "default" << endl; };
};

template<typename A>
class C<A,CObject>
{
public:
  void foo() { cout << "CObject" << endl; };
};

要在Visual C 2008专业化成员函数,您可以使其模板:

template<typename A,typename B>
class C
{
  template<typename T>
  void foo();

  template<>
  void foo<CObject>();
};

上述解决方案似乎仅在将来的C标准(根据草案n2914 14.6.5.3/2)才可用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读