如何在c ++中转发声明别名模板

时间:2016-06-28 06:38:59

标签: c++ templates

我有以下别名模板:

#include <vector>

template <typename T>
using MyVector = std::vector <T>;

我如何转发声明MyVector

template <typename T>
class MyVector;

对我不起作用。

2 个答案:

答案 0 :(得分:1)

您无法转发声明使用声明 无论如何,您可以按照以下方式声明一种 traits

#include <type_traits>
#include <vector>

template<typename>
struct MyStuff;

template<typename T>
auto f() { return typename MyStuff<T>::MyVector{}; }

template<typename T>
struct MyStuff {
    using MyVector = std::vector<T>;
};

int main() {
    static_assert(std::is_same<decltype(f<int>()), std::vector<int>>::value, "!");
}

答案 1 :(得分:1)

你可以将它变成一种类型,你可以向前宣布:

template <typename T>
struct MyVector: std::vector<T> {};