Search code examples
c++11member-initializationvisual-c++-2013anonymous-struct

Brace-or-equal-initializers in anonymous struct does not work on VS2013


Brace-or-equal-initializers in an anonymous struct within a struct doesn't do their work on output produced by VS2013. There's the code:

#include <iostream>
#include <cstdint>


struct S
{
    struct
    {
        uint64_t val = 0;
    }anon;
};

int main()
{
    S s;
    S *a = new S;

    std::cout << s.anon.val << std::endl;
    std::cout << a->anon.val << std::endl;

    return 0;
}

Compile with this command on Linux:

g++ -std=c++11 def-init-anon-atruct.cpp -o def-init-anon-atruct

(Adding optimisation flags does not affect the result)

Expected result:

0
0

Weird. Running that with VS2013 gives garbage values. Who's right on this one in terms of implementing C++11 standards right? I highly doubt this is GCC's fault.

Is it something to do with some useless VS compiler options? Windows extensions? I have to make default constructors for the structures because of a bug MS made? this is absurd.


Solution

  • Non-static data member initialisers being silently ignored in nested anonymous structs is a confirmed bug in Visual C++ 2013, fixed in Visual C++ 2015 RTM.