为什么带有 in-class 初始值设定项的局部结构隐藏 class' 变量?

Why does local struct with in-class initializer hide class' variables?

考虑这段代码:

class A
{
public:
  void f();

private:
  int foo;
};

void A::f( )
{
  struct S
  {
    int bar = 100;
  } s;

  s.bar = foo;
}

这不会用 VS2013 编译,在最后一行给出 C2327 和 C2065 错误。但是,如果我们删除初始化程序或将结构声明放在外面,这将编译。这是错误还是标准行为?
编辑: 只是为了完成这里的问题是错误消息:

error C2327: 'A::foo' : is not a type name, static, or enumerator
error C2065: 'foo' : undeclared identifier

试一试。

class A
{
public:
    void f();

private:
    int foo;
};

void A::f( )
{
    struct S
    {
        int bar;
        S() { bar = 100; }
    } s;

    s.bar = foo;
}

编辑:仅供参考,您正在尝试在本地 class.

中初始化一个非常量静态成员

您的代码完全合法。我猜想 MSVC 在 "new"(即 C++11)"in class initializer" int bar = 100;.

方面有问题

要么使用更现代的编译器,要么,如果您坚持使用 MSVC 2013,要么编写 C++98,要么准备好为正确的代码获取随机编译错误。

代码适用于 VS2015 RC(最终版)。所以 VS 2013 在 class 初始化中有一个错误。以下代码适用于 VS 2013

class A
{
public:
    void f();
private:
    int foo;
};

void A::f( )
{
    struct S
    {
        int bar;
        S(int b = 0) : bar{b} {}
    } s;

    s.bar = foo;
}