用于派生特定类型的函数模板专门化
Function template specialization for derivation of a specific type
class base
{
};
class derived
{
};
template<class T> void foo() {}
int main()
{
foo<int>();
foo<derived>();
}
我想对foo
专门化,因为T = base
的导数。这是可能的还是我需要专门为base
本身?
您可以将boost::enable_if和boost::is_base_of组合使用,如boost::enable_if的手册所述。
template <class T>
T foo(typename enable_if<boost::is_base_of<base,T> >::type* dummy = 0);
一种选择是使用boost类型特征(或者如果觉得勇敢,请查看该库的源代码)。
在编译时,您可以检测一个类型是否从另一个类型继承,从而选择适当的实现。
相关文章:
- 错误 C2893 无法专门化函数模板'unknown-type std::invoke(_Callable &&,_Types &&...)'
- 从 std 命名空间中专门化函数模板的想法有多糟糕?
- 如何专门化模板构造函数模板
- 专门化采用通用引用参数的函数模板
- 使用decltype尾部返回类型专门化函数模板
- 为模板化派生类专门化函数模板
- 如何专门化容器的函数模板
- 显式函数模板专门化选择了错误的专门化
- C++模板-专门化函数
- 在单独的.cpp文件中专门化函数模板
- 成员和成员函数的模糊部分模板专门化
- C++中模板类的成员函数的专门化
- 如何专门化具有多个参数的函数模板
- 函数模板专门化
- 二进制搜索的一个函数模板专门化
- 当函数模板专门化到不同的命名空间时,GCC和clang不同意
- 构造函数模板专门化对析构函数给出了未定义的引用
- 用于派生特定类型的函数模板专门化
- 有没有一种方法可以为函数模板专门化命名类型
- 使用C调用约定调用函数模板专门化