c++ - 为什么我不能在函数内部声明模板化类型别名?

标签 c++ templates typedef c++14 template-aliases

<分区>

为什么我不能在函数内部声明模板化类型别名?

#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

为什么我们被迫将这些声明放在 block 作用域之外?

#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 ]

关于c++ - 为什么我不能在函数内部声明模板化类型别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34419603/

相关文章:

c++ - 如何停止对辅助隐式加载 DLL 的 DLL 劫持

c++ - 如何选择像魔杖一样的区域并使用 OpenCV 对其进行洪水填充?

c++ - 重载数组的输出运算符

c++ - 完美的一对

c - 在 C 中定义整数范围

c++ - 无法使用 bash 脚本正确重启应用程序

c++ - 可以使用 std::function typedef 来帮助定义 lambda 吗?

c++ - 制作一个优化未使用部分的元组样式类

c - C typedef 结构中定义之前名称的用途

c++ - Arduino 上的结构 : function() 'does not name a type'