为什么代码“foo :: foo :: foo :: foob”编译?

时间:2017-10-18 08:05:56

标签: c++ c++11 gcc language-lawyer names

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

li p, li h1, li h2 {
    margin-top: 0;
    margin-bottom: 0;
    padding-top: 0;
    padding-bottom: 0;
}

GCC 5.1.0编译了这个。

这个编译的规则是什么?

2 个答案:

答案 0 :(得分:30)

此处使用injected-class-name

  

为了lookup的目的,其自己定义中的类的名称充当其自身的公共成员类型别名(除非用于命名constructor):这称为注入 - 类名

然后

@Document(collection = "clients")
public class ClientData {

    @Id
    private String id;
    private double bearing;
    private double latitude;
    private double velocity;
    private double longitude;
    private LocationBearerType clientDataType = CAR;
    private Boolean standby;
    private long timestamp;

即。 foo:: foo:: foo::foob foo::foo::foo::foob相同。

然后foo::foobrange-based for loop (since C++11),它会迭代3个枚举数形成的braced-init-list

答案 1 :(得分:4)

我将此代码更改为:

#include <initializer_list>
#include <iostream>
struct foo {
  foo() : baz(foobar) {}
  enum bar {foobar, fbar, foob};
  bar baz;
};

int main() {
  for( auto x : { foo::foobar,
                  foo::fbar,
                  foo::
                  foo::
                  foo::foob } )
                  {
                      std::cout << "x=" << x << std::endl;
                  }
  return 0;
}

for循环运行3次。输出为:“x = 1 x = 2 x = 3”。

foo::foo::foo::foobfoo::foob相同。 所以

for( auto x : { foo::foobar,
                  foo::fbar,
                  foo::
                  foo::
                  foo::foob } )

是相同的

for( auto x : { foo::foobar, foo::fbar, foo::foob } )
{
}

这意味着x的范围为{ foo::foobar, foo::fbar, foo::foob }

相关问题