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

template<typename T>
struct X{};

template<>
struct X<int>{};

C++ 11为我们提供了用于表达模板typedef的新酷using语法:
template<typename T>
using YetAnotherVector = std::vector<T>

有没有办法使用类似于结构模板特化的结构来为这些模板定义模板特化?我尝试了以下方法:
template<>
using YetAnotherVector<int> = AFancyIntVector;

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

最佳答案

不。

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

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

希望能有所帮助。

关于c++ - 使用模板特化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26844443/

10-11 23:22
查看更多