Matthew Matthew - 3 years ago 141
C++ Question

Cannot declare a templated type alias in a function

I'm attempting to do

template <typename N>
using Array = std::array<std::uint8_t, N>;


Within a function, but clang spits out an error: expected expression.

For example

void func()
{
template <typename T>
using Vec = std:vector<T>;
}


will result in this error.

Is this not allowed in functions and if so why? Thanks

Answer Source

Yes, alias template could only be declared at class scope or namespace scope.

Like any template declaration, an alias template can only be declared at class scope or namespace scope.

BTW: You should declare a non-type template parameter for your alias template Array, e.g.

template <std::size_t N>
using Array = std::array<std::uint8_t, N>;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download