基于基类的专用成员函数
specialized member function based on baseclass
这个问题类似于:所有子类的 C++ 模板专用化而不是模板化函数,现在我有一个模板化类的成员函数,它需要根据类模板的基类做不同的事情
template<typename T>
class xyz
{
void foo()
{
if (T is a subclass of class bar)
do this
else
do something else
}
}
我找不到一个易于理解的 boost::enable_if 教程,所以我无法为这个小修改获得正确的语法
您可以使用标签调度:
template <typename T> class xyz
{
public:
void foo()
{
foo( typename boost::is_base_of<bar,T>::type());
}
protected:
void foo( boost::mpl::true_ const& )
{
// Something
}
void foo( boost::mpl::false_ const& )
{
// Some other thing
}
};
请注意,标记调度通常比使用 enable_if 的 SFINAE 更好,因为enable_if在选择适当的重载之前需要线性数量的模板实例化。
在 C++11 中,您可以使用等效于这些 boost 元函数的 std:: 。
相关文章:
- 静态数据成员模板专用化的实例化点在哪里
- 成员变量如何使用专用类模板?
- GCC 7 中模板类的模板成员函数的专用化
- C++ 类模板部分专用化,而不专用化所有成员函数
- 专用于可变参数模板成员函数
- 对专用模板成员的未定义引用
- 对类模板成员的显式专用化的约束
- 具有专用成员函数的默认模板参数
- 如何正确地显式实例化具有完全专用成员的模板类
- C++模板类中的非专用成员
- 运算符<<重载时无法访问专用成员(指定指针)
- 避免强制转换以访问派生类的专用成员
- 枚举类型参数的专用成员模板
- 组织专用成员 Vector<Vector 接口的最佳方式<Type>>
- 基于基类的专用成员函数
- C++模板:专用成员函数,用于解决主模板中不明确的重载情况
- 导出/定义静态模板专用成员变量C++
- 无法访问QXmlStreamReader的专用成员(运算符=)
- 类专用成员-数组访问失败
- C++中非模板类的专用成员函数