有什么方法可以只为基本类型制作模板特化吗? 我已尝试执行以下操作:
template<typename T, typename = typename std::enable_if<!std::is_fundamental<T>::value>::type>
class foo
{
}
template<typename T, typename = typename std::enable_if<std::is_fundamental<T>::value>::type>
class foo
{
}
但是我得到一个模板已经定义的错误。
最佳答案
这里您创建了两个同名的模板类,而不是特化类。
您需要创建一个通用的然后专门化它:
// not specialized template (for non-fundamental types), Enabler will
// be used to specialize for fundamental types
template <class T, class Enabler = void>
class foo { };
// specialization for fundamental types
template <class T>
class foo<T, std::enable_if_t<std::is_fundamental<T>::value>> { };
关于c++ - 基本类型的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45244809/