user350954
user350954

Reputation: 329

In C++ how to declare an alias for std::array?

In C++/C++11, how do we declare an alias for std::array?

I mean something like this:

template<size_t N> using Array = array<int, N>;

int get(Array A, int index) {
    return A[index];
}

But this result in compile error: Array is not a type.
What's the correct way? Many thanks.

Upvotes: 1

Views: 1059

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477160

Since your alias is a template, the get function needs to be a template, too:

template <size_t N> int get(Array<N> const & a, int index)
{
    return a[index];
}

You can of course do this more generally for the original array template, too:

template <typename T, size_t N>
T & get(std::array<T, N> & a, int n)
{ return a[n]; }

Upvotes: 6

Related Questions