Jonathan
Jonathan

Reputation: 31

How to forward declare an aliased template in c++

I have the following aliased template:

#include <vector>

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

How can i forward declare MyVector?

template <typename T>
class MyVector;

does not work for me.

Upvotes: 2

Views: 1615

Answers (2)

AlwaysLearning
AlwaysLearning

Reputation: 8011

You might be able to get away with making it a type, which you will be able to forward-declare:

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

Upvotes: 2

skypjack
skypjack

Reputation: 50540

You cannot forward declare an using declaration.
Anyway, you can declare forward a sort of traits as it follows:

#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, "!");
}

Upvotes: 1

Related Questions