gpt4 book ai didi

c++ - 同名的 typedef 和模板参数

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

为什么这种情况不正确(这是合乎逻辑的)

template <typename T>
struct Der: public Base
{
typedef int T;
T val;
};

,但这种情况是正确的吗?

struct Base
{
typedef int T;
};

template <typename T>
struct Der: public Base
{
T val;
};

标准 14.6.1/7 说:

In the definition of a class template or in the definition of a member of such a template that appears outside of the template definition, for each base class which does not depend on a template-parameter (14.6.2), if the name of the base class or the name of a member of the base class is the same as the name of a template-parameter, the base class name or member name hides the template-parameter name (3.3.7).

为什么这里没有歧义?

最佳答案

根据 [temp.local]/6,第一个例子是不正确的:

A template-parameter shall not be redeclared within its scope (including nested scopes).

然而,在

template <typename T>
struct Der: public Base
{
T val;
};

T 被继承自 Base 的名称隐藏 - 正如您的引用所指定的那样。

[..] if the name of the base class or the name of a member of the base class is the same as the name of a template-parameter, the base class name or member name hides the template-parameter name (3.3.7).

也就是说,成员val的类型是intDemo .

关于c++ - 同名的 typedef 和模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28029722/

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