我试图找出如何对类成员函数进行模板特化,这不仅适用于模板特化类A,而且适用于来自类A的所有派生类。
类似的问题已经被问到here,但是解决方案使用了重载而不是specilizes,所以我看不出如何调整我的问题的答案(因为我不想触摸函数签名)。
我的情况是,我有一个复杂的课程,我不想触摸
template< class T >
class Complex :
public MoreComplex
{
public:
//... more stuff here...
virtual bool Start();
};
template< class T >
bool Complex< T>::Start()
{
//do some stuff
return true;
}
在我使用的框架中,通过对成员函数进行模板特化来使用此类。 例如,当有A类时:
template< >
inline bool Complex<A>::Start()
{
//do special stuff only for class A
return true;
}
到目前为止,这么简单。现在我希望从类A派生的所有类都使用Start函数的相同实现。如何才能做到这一点?
在做了一些搜索之后,我找到了SFINAE的概念并尝试了以下内容:
template< class T >
inline bool Complex< typename std::conditional<std::is_base_of<A, T>::value, A, void>::type >::Start()
{
//do some a class stuff
return true;
}
这不起作用(甚至不构建)。我可以为您提供更多详细信息,但我想答案可能很简单,我对模板和SFINAE的了解也很低。