【问题标题】:Fields with the same name across different anonymous structures inside on union联合内部不同匿名结构中具有相同名称的字段
【发布时间】:2018-11-14 22:15:28
【问题描述】:

在一个 union 内的不同匿名结构中具有相同名称的字段是否合法?

union Foo
{
    struct
    {
        int bar;
    };

    struct
    {
        int bar;
    };
};

MSVC 实际上允许这样做,而 GCC 则不允许。

【问题讨论】:

标签: c++


【解决方案1】:

代码无效,仅仅是因为 C++ 没有匿名结构。

使用-pedantic-errors,GCC 拒绝使用error: ISO C++ prohibits anonymous structs [-Wpedantic] 编译它。

【讨论】:

  • 我刚刚更新了我的问题,请看一下新版本。
  • @FrozenHeart 我会说它需要一个单独的问题,可能标记为language-lawyer。但由于 GCC 和 Clang 都拒绝新的 sn-p,我闻到了 MSVC 的错误。
猜你喜欢
  • 1970-01-01
  • 2021-06-07
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 2017-10-25
  • 2016-08-25
  • 2016-09-09
  • 2019-10-14
相关资源
最近更新 更多