【发布时间】:2014-11-10 13:00:42
【问题描述】:
通常的模板结构可以被特化,例如,
template<typename T>
struct X{};
template<>
struct X<int>{};
C++11 为我们提供了新的很酷的 using 语法来表达模板类型定义:
template<typename T>
using YetAnotherVector = std::vector<T>
有没有办法为这些使用类似于结构模板的特化的构造来定义模板特化?我尝试了以下方法:
template<>
using YetAnotherVector<int> = AFancyIntVector;
但它产生了编译错误。这有可能吗?
【问题讨论】:
-
AFAIK,你确实需要一个后端类。隐藏专门的
struct,然后使用该类创建别名。 -
不确定我是否关注,
typedef YetAnotherVector<int> AFancyIntVector有什么问题? -
@Mr.kbok:将您的语句与 using 一起使用会导致编译错误“多个类型在一个声明中”
-
@Mr.kbok,OP 希望
YetAnotherVector<T>成为std::vector<T>,但YetAnotherVector<int>成为AFancyIntVector。 -
chris:好的,我反其道而行之。 :)
标签: c++ templates c++11 template-specialization