2

It took me a good hour to locate this issue. The following code

class Test {
public:
    void method();

    int _member;
};

void Test::method()
{
    struct S {
        int s = 0; // same with int s {0};
    };

    _member;
}

int main(int argc, const char* argv [])
{
    return 0;
}

Produces a compilation error:

1>error C2327: 'Test::_member' : is not a type name, static, or enumerator
1>error C2065: '_member' : undeclared identifier

And the error goes away as soon as I replace int s = 0; with int s;.

This only occurs in MSVC 2013, not 2015. I'm pretty sure it's a compiler bug but I want to make sure it's not some C++ peculiarity that I'm not familiar with (something that changed between C++11 and C++14).

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • This compiles fine in G++-5.3.0 too. – Mattia F. Jul 07 '16 at 14:37
  • 2
    Looks like a bug. g++ and clang both compile fine and MSVS 2013 didn't have the best support for C++11. – NathanOliver Jul 07 '16 at 14:38
  • From what I can tell, MSVC 2013 is supposed to support non-static member initialization. But I only seem their examples using brace initialization. Did you try `int s{0};`? – Fred Larson Jul 07 '16 at 14:43
  • @NathanOliver: do GCC and clang compile this with `-std=c++11`? It's not like MSVC 2013 doesn't support in-place initialization; it just produces a weird error when you use that in a **method-local class**. – Violet Giraffe Jul 07 '16 at 14:43
  • @FredLarson: yes, it's in the comment to the source code in my Q. – Violet Giraffe Jul 07 '16 at 14:44
  • I missed that. Sorry. – Fred Larson Jul 07 '16 at 14:44
  • 1
    @VioletGiraffe [g++](http://coliru.stacked-crooked.com/a/d93024647a0c8307) and [clang](http://coliru.stacked-crooked.com/a/ce0644869a242f22) both compile with -std=c++11 – NathanOliver Jul 07 '16 at 14:45
  • It is a bug in the parser. Not terribly surprising, they had to rewrite the entire front-end to get C++1x compatibility. Sure, a lot of those bugs were fixed in 2015, reporting the bug isn't useful. – Hans Passant Jul 07 '16 at 15:20

1 Answers1

1

[C++11: 12.6.2] defines NSDMIs in C++11, and neither this section nor any other section in the document defines such a constraint on the syntax. Therefore, it must be an implementation bug.

And, since GCC, Clang and Visual Studio 2015 all accept the code, I don't think any more detailed investigation is worthwhile.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055