基本类型的模板专业化 [英] Template specialization for fundamental types

查看:76
本文介绍了基本类型的模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以仅对基本类型进行模板专业化吗? 我尝试执行以下操作:

Is there any way to make a template specialization for fundamental types only? I have tried to do the following:

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
{
}

但是我收到一个错误消息,即模板已经定义.

But I'm getting an error that the template is already defined.

推荐答案

在这里,您将创建两个具有相同名称而不是专业化的模板化类.

Here you are creating two templated classes with the same name, not specializations.

您需要创建一个通用的,然后对其进行专门化:

You need to create a generic one and then specialize it:

// 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>> { };

这篇关于基本类型的模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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