読者です 読者をやめる 読者になる 読者になる

VC2013が無名構造体のメンバに初期値を記述しても初期化してくれない

VisualStudio2013のC++11実装が甘い。

// hoge.cpp
#include <stdio.h>

struct Hoge {
    int one = 11;
    int two = 12;
    int sun = 13;

    struct named_t {
        int one = 21;
        int two = 22;
        int sun = 23;
    } named;

    struct {
        int one = 31;
        int two = 32;
        int sun = 33;
    } noname;
};

int
main(int, const char**) {
    Hoge hoge;
    printf("hoge.one = %d\n", hoge.one);
    printf("hoge.two = %d\n", hoge.two);
    printf("hoge.sun = %d\n", hoge.sun);
    printf("hoge.named.one = %d\n", hoge.named.one);
    printf("hoge.named.two = %d\n", hoge.named.two);
    printf("hoge.named.sun = %d\n", hoge.named.sun);
    printf("hoge.noname.one = %d\n", hoge.noname.one);
    printf("hoge.noname.two = %d\n", hoge.noname.two);
    printf("hoge.noname.sun = %d\n", hoge.noname.sun);
    printf("ok.\n");
    return 0;
}
# SConstruct
Program('hoge.cpp',CCFLAGS=['/W4','/WX'])
$ scons -Q hoge.exe && ./hoge
cl /Fohoge.obj /c hoge.cpp /TP /W4 /WX
Microsoft(R) C/C++ Optimizing Compiler Version 18.00.31101 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

hoge.cpp
link /nologo /OUT:hoge.exe hoge.obj
hoge.one = 11
hoge.two = 12
hoge.sun = 13
hoge.named.one = 21
hoge.named.two = 22
hoge.named.sun = 23
hoge.noname.one = 0
hoge.noname.two = 0
hoge.noname.sun = 0
ok.

$ g++ --version
g++ (GCC) 4.9.3
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -Wall -std=c++11 -o hoge hoge.cpp && ./hoge
hoge.one = 11
hoge.two = 12
hoge.sun = 13
hoge.named.one = 21
hoge.named.two = 22
hoge.named.sun = 23
hoge.noname.one = 31
hoge.noname.two = 32
hoge.noname.sun = 33
ok.

警告も出さないとかマジかVC…。初期値記述するときは無名構造体を使うのはやめよう。