使用模板专业化 [英] using template specialization

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

问题描述

通常的模板结构可以是专门的,例如

Usual template structs can be specialized, e.g.,

template<typename T>
struct X{};

template<>
struct X<int>{};

C ++ 11为我们提供了新的很酷的 using 语法,用于表达模板typedef:

C++11 gave us the new cool using syntax for expressing template typedefs:

template<typename T>
using YetAnotherVector = std::vector<T>

有没有一种方法可以使用类似于结构模板特化的结构来定义这些模板的特化?我尝试了以下方法:

Is there a way to define a template specialization for these using constructs similar to specializations for struct templates? I tried the following:

template<>
using YetAnotherVector<int> = AFancyIntVector;

但是它产生了一个编译错误.这有可能吗?

but it yielded a compile error. Is this possible somehow?

推荐答案

否.

但是您可以将别名定义为:

But you can define the alias as:

template<typename T>
using YetAnotherVector = typename std::conditional<
                                     std::is_same<T,int>::value, 
                                     AFancyIntVector, 
                                     std::vector<T>
                                     >::type;

希望有帮助.

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

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