为什么我不能在函数内部声明模板化类型别名?
Why can't I declare templated type aliases inside of functions?
为什么我不能在函数内部声明模板化类型别名?
#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
为什么我们被迫将这些声明放在块作用域之外?
#include <vector>
//type alias deceleration:
template <typename T>
using type = std::vector<T>;
int main(){
//type instantiation:
type<int> t;
}
标准是这么说的。
来自 C++11 标准(强调我的):
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 ]
为什么我不能在函数内部声明模板化类型别名?
#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
为什么我们被迫将这些声明放在块作用域之外?
#include <vector>
//type alias deceleration:
template <typename T>
using type = std::vector<T>;
int main(){
//type instantiation:
type<int> t;
}
标准是这么说的。
来自 C++11 标准(强调我的):
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 ]