C ++部分方法专门化 [英] C++ partial method specialization

查看:153
本文介绍了C ++部分方法专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有模板类方法的部分专门化?

Is there a partial specialization for template class method?

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

它无法像这样专门化:

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

任何帮助?

推荐答案

如果你已经有专门的类,你可以在专门的类中给出 foo 的不同实现:

If you are already have specialized class you could give different implementation of foo in specialized class:

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中,你也可以使它模板:

To specialize member function in Visual C++ 2008 you could make it template too:

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

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

上述解决方案似乎只有在未来的C ++标准中才可用(根据draft n2914 14.6.5.3 / 2)。

The solution above seems to will be available only in future C++ Standard (according to draft n2914 14.6.5.3/2).

这篇关于C ++部分方法专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆