为什么代码"foo :: foo :: foo :: foob"编译? [英] Why is the code "foo::foo::foo::foob" compiling?

查看:115
本文介绍了为什么代码"foo :: foo :: foo :: foob"编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A co-worker accidentally wrote code like this:

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对此进行了编译.

GCC 5.1.0 compiles this.

进行此编译的规则是什么?

What's the rule that makes this compile?

推荐答案

注入了-此处使用类名

出于查找(除非用于命名构造函数):这称为注入类名称

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 })基于范围的for循环(因为C ++ 11),它会在 braised-init-list 由3个枚举数组成.

And then for (auto x : {foo::foobar, foo::fbar, foo::foob }) is a range-based for loop (since C++11), which iterates on the braced-init-list formed by the 3 enumerators.

这篇关于为什么代码"foo :: foo :: foo :: foob"编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆