gpt4 book ai didi

c++ - 为什么编译代码 "foo::foo::foo::foob"?

转载 作者:IT老高 更新时间:2023-10-28 23:09:15 29 4
gpt4 key购买 nike

一位同事不小心写了这样的代码:

struct foo {
foo() : baz(foobar) {}
enum bar {foobar, fbar, foob};
bar baz;
};

void f() {
for( auto x : { foo::foobar,
foo::fbar,
foo::
foo::
foo::foob } );
// ...
}

GCC 5.1.0 编译这个。

编译的规则是什么?

最佳答案

injected-class-name这里用到了,

the name of the class within its own definition acts as a public member type alias of itself for the purpose of lookup (except when used to name a constructor): this is known as injected-class-name

然后

foo::
foo::
foo::foob

foo::foo::foo::foobfoo::foob 相同。

然后 for (auto x : {foo::foobar, foo::fbar, foo::foob })range-based for loop (since C++11) , 迭代 braced-init-list由 3 个枚举数组成。

关于c++ - 为什么编译代码 "foo::foo::foo::foob"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46805449/

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