【问题标题】:Brace-or-equal-initializers in anonymous struct does not work on VS2013匿名结构中的 Brace-or-equal-initializers 在 VS2013 上不起作用
【发布时间】:2017-07-17 14:17:00
【问题描述】:

结构中的匿名结构中的大括号或等号初始化器不会对 VS2013 生成的输出执行其工作。代码如下:

#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;
}

在 Linux 上使用此命令编译:

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

(添加优化标志不影响结果)

预期结果:

0
0

很奇怪。用 VS2013 运行它会给出垃圾值。在实现 C++11 标准方面谁是正确的?我非常怀疑这是 GCC 的错。

是否与一些无用的 VS 编译器选项有关? Windows 扩展?由于 MS 的错误,我必须为结构创建默认构造函数?这太荒谬了。

【问题讨论】:

标签: c++11 member-initialization visual-c++-2013 anonymous-struct


【解决方案1】:

由于 MS 的错误,我必须为结构创建默认构造函数?这太荒谬了。

是的,不,这并不荒谬。

编译器也是程序,并且往往存在错误——比其他的要多。

如果您对该工具别无选择,则必须克服该工具的局限性,无论理论上听起来多么荒谬。

【讨论】:

    【解决方案2】:

    在嵌套匿名结构中被静默忽略的非静态数据成员初始化器在 Visual C++ 2013 中是 confirmed bug,在 Visual C++ 2015 RTM 中是 fixed

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 2019-09-28
      • 1970-01-01
      • 2011-05-11
      • 2014-01-14
      • 2020-07-12
      相关资源
      最近更新 更多