gpt4 book ai didi

c++ - 在 C++ 模板类中,我可以使用相同的名称对模板参数进行类型定义吗?

转载 作者:可可西里 更新时间:2023-11-01 15:24:26 26 4
gpt4 key购买 nike

如果我有一个模板类:

template<typename Layout>
class LayoutHandler : Handler {
};

并且我想将参数 Layout 公开给类的用户。然后:

template<typename Layout>
class LayoutHandler : Handler {
public:
typedef Layout Layout; // using the same name
};

VS2012 可以编译这段代码,并给出预期的结果。 (我使用 std::is_same 来检查它。)这在标准 C++03 或 C++11 中是否允许?

最佳答案

在 C++11 中是不允许的。

typedef 是一个声明。 (见 7.1.3 节)

template 参数不能在其范围内(包括嵌套范围)重新声明。 (见 14.6.1.6 节)

C++11 draft standard n3242

关于c++ - 在 C++ 模板类中,我可以使用相同的名称对模板参数进行类型定义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16613773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com