gpt4 book ai didi

c++ - -Wshadow=global 将枚举类条目视为全局阴影。为什么?

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

在 GCC 7.3 和 8.2 上使用 -Wshadow=global 进行编译时,编译器会警告以下代码片段有阴影。

constexpr int A = 0;

class Bar {
public:
enum Bars {
A = 0
};
};

enum class Foo {
A = 0 // warns this entry shadows global declaration of A
};

int main() {
return 0;
}

<source>:11:9: warning: declaration of 'A' shadows a global declaration [-Wshadow]
A = 0
^
<source>:1:15: note: shadowed declaration is here
constexpr int A = 0;
^

因为枚举类在引用时需要枚举类名,我的理解是 A 的所有三个声明都是分开的:::A::Bar::A::Foo::A

Clang 7 不会发出带有 -Wshadow 的警告。

这是有效的阴影警告吗?如果是,为什么?

最佳答案

关于此问题的错误已提交,标题为 "-Wshadow generates an incorrect warning with enum classes" .但是,尚未确认这是一个错误。

Jonathan Wakely认为这不是错误,并给出了以下示例。

typedef unsigned char foo;
enum class myenum
{
foo,
bar = (foo)-1
};

Is the value -1L or 255?

If I rename myenum::foo to myenum::Foo the code silently changes meaning.

It also changes meaning if I reorder the declarations of myenum::foo and myenum::bar, which is exactly the sort of fragile code that deserves a warning.

问题中发布的示例也是如此。如果全局 int Aenum class Foo 之后 声明,则不再有警告。

另一个用户同意该主题:

In an ideal world we would only warn when ambiguity exists (in the user mind), that is, at "bar = (foo) -1". However, that is probably much more difficult and expensive than the current warning.

关于c++ - -Wshadow=global 将枚举类条目视为全局阴影。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53841625/

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