Why can't I declare a templated type alias inside of a function?
#include <vector>
int main(){
//type alias deceleration:
template <typename T>
using type = std::vector<T>;
//type instantiation:
type<int> t;
}
error: a template declaration cannot appear at block scope
Why are we forced to put these declarations outside of block scope?
#include <vector>
//type alias deceleration:
template <typename T>
using type = std::vector<T>;
int main(){
//type instantiation:
type<int> t;
}
The standard says so.
From the C++11 Standard (emphasis mine):
14 Template
2 A template-declaration can appear only as a namespace scope or class scope declaration. In a function template declaration, the last component of the declarator-id shall not be a template-id. [ Note: That last component may be an identifier, an operator-function-id, a conversion-function-id, or a literal-operator-id. In a class template declaration, if the class name is a simple-template-id, the declaration declares a class template partial specialization (14.5.5). —end note ]