Search code examples
c++templatesc++11stlusing

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.


Solution

  • 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]; }