Auloma
Auloma

Reputation: 153

Force stl container size and declare it as a type

I want to define a std::vector with 2 elements as a type, and export it. Something looking like:

template <class T>
using Vec2 = std::vector<T>(2);

Edit: A lot of interesting responses. Thanks to everyone, I will use a typedef struct because I don't really need to access container's member functions.

Upvotes: 0

Views: 106

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122575

No. This is not possible because a std::vector<int> of size 2 has the same type as a std::vector<int> of size 42. Vectors are resizable, thats basically what makes them vectors and distinguishes them from std::array. For a container of fixed size 2 you can use arrays:

template <class T>
using Vec2 = std::array<T,2>;

Upvotes: 3

Related Questions