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

2

9

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.

Eva answered 27/2, 2017 at 4:18 Comment(10)
#1070121Houdini
I also highly doubt this is gcc's fault.Dogma
Intel's compiler offers better language conformance on Microsoft target platforms.Annemarie
Visual Studio 2017 (Compiler v141) gives the expected result: i.imgur.com/PcddbQ2.pngLycanthrope
What's VS2013's output if you add anon = {};?Separate
@Separate That's not the point here. If I wanted to initialise the structure with additional codes outside the structure, I wouldn't be posting this.Eva
That's to also @Ben. I don't think many people here actually read questions before they write anything.Eva
Support ended. No bug to file. support.microsoft.com/en-us/help/14086/…Eva
Visual Studio 2015 Update 3 gives the expected result too.Meliamelic
Good argument for your boss to get a new compiler ;-)Lulululuabourg
T
1

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.

Truncheon answered 7/3, 2017 at 20:55 Comment(0)
C
6

I have to make default constructors for the structures because of a bug MS made? this is absurd.

Yes, and no, it is not absurd.

Compilers are programs too, and tend to have bugs -- some more than others.

If you don't have a choice about the tool, you have to work with the limitations of the tool, no matter how absurd it sounds in theory.

Celery answered 2/3, 2017 at 5:41 Comment(0)
T
1

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.

Truncheon answered 7/3, 2017 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.