为什么 C++ 不允许匿名结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2253878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:43:49  来源:igfitidea点击:

Why does C++ disallow anonymous structs?

c++structunions

提问by Adrian McCarthy

Some C++ compilers permit anonymous unions and structs as an extension to standard C++. It's a bit of syntactic sugar that's occasionally very helpful.

一些 C++ 编译器允许匿名联合和结构作为标准 C++ 的扩展。这是一些偶尔非常有用的语法糖。

What's the rationale that prevents this from being part of the standard? Is there a technical roadblock? A philosophical one? Or just not enough of a need to justify it?

阻止它成为标准一部分的理由是什么?有技术障碍吗?一个哲学的?或者只是没有足够的必要来证明它的合理性?

Here's a sample of what I'm talking about:

这是我正在谈论的示例:

struct vector3 {
  union {
    struct {
      float x;
      float y;
      float z;
    };
    float v[3];
  };
};

My compiler will accept this, but it warns that "nameless struct/union" is a non-standard extension to C++.

我的编译器会接受这一点,但它警告说“无名结构/联合”是 C++ 的非标准扩展

采纳答案by bames53

As others have pointed out anonymous unions are permitted in standard C++, but anonymous structs are not.

正如其他人指出的那样,标准 C++ 中允许匿名联合,但匿名结构不允许。

The reason for this is that C supports anonymous unions but not anonymous structs*, so C++ supports the former for compatibility but not the latter because it's not needed for compatibility.

这样做的原因是 C 支持匿名联合但不支持匿名结构*,因此 C++ 支持前者是为了兼容性,但不支持后者,因为兼容性不需要。

Furthermore, there's not much use to anonymous structs in C++. The use you demonstrate, to have a struct containing three floats which can be referred to either by .v[i], or .x, .y, and .z, I believe results in undefined behavior in C++. C++ does not allow you to write to one member of a union, say .v[1], and then read from another member, say .y. Although code that does this is not uncommon it is not actually well defined.

此外,C++ 中的匿名结构没有多大用处。您演示的使用,有一个包含三个浮点数的结构,可以由.v[i], 或.x, .y, 和引用.z,我相信会导致 C++ 中的未定义行为。C++ 不允许您写入联合的一个成员,例如.v[1],然后从另一个成员读取,例如.y。尽管执行此操作的代码并不少见,但实际上并没有很好地定义。

C++'s facilities for user-defined types provide alternative solutions. For example:

C++ 的用户定义类型工具提供了替代解决方案。例如:

struct vector3 {
  float v[3];
  float &operator[] (int i) { return v[i]; }
  float &x() { return v[0]; }
  float &y() { return v[1]; }
  float &z() { return v[2]; }
};

* C11 apparently adds anonymous structs, so a future revision to C++ may add them.

* C11 显然添加了匿名结构,因此 C++ 的未来修订版可能会添加它们。

回答by bobobobo

I'll say, you can clean up your vector3declaration by just using a union

我会说,您可以vector3通过使用union

union vector3 {
  struct { float x, y, z; } ;
  float v[3] ;
} ;

Sure, anonymous structures wasan MSVC extension. But ISO C11 permits it now, and gcc allows it, and so does Apple's llvm compiler.

当然,匿名结构MSVC 扩展。但是 ISO C11 现在允许它gcc 允许它,Apple 的 llvm 编译器也是如此。

Why in C11 and not C++11? I'm not sure, but practically speaking most (gcc++, MSVC++ and Apple's C++ compiler) C++ compilers support them.

为什么是 C11 而不是 C++11?我不确定,但实际上大多数(gcc++、MSVC++ 和 Apple 的 C++ 编译器)C++ 编译器都支持它们。

回答by Dan

Not sure what you mean. Section 9.5 of the C++ spec, clause 2:

不明白你的意思。C++ 规范的第 9.5 节,第 2 条:

A union of the form

union { member-specification } ;

is called an anonymous union; it defines an unnamed object of unnamed type.

形式的联合

union { member-specification } ;

称为匿名联合;它定义了一个未命名类型的未命名对象。

You can do things like this too:

你也可以做这样的事情:

void foo()
{
  typedef
  struct { // unnamed, is that what you mean by anonymous?
    int a;
    char b;
  } MyStructType; // this is more of a "C" style, but valid C++ nonetheless

  struct { // an anonymous struct, not even typedef'd
    double x;
    double y;
  } point = { 1.0, 3.4 };
}

Not always very useful... although sometimes useful in nasty macro definitions.

并不总是很有用...虽然有时在讨厌的宏定义中很有用。

回答by David Thornley

Unions can be anonymous; see the Standard, 9.5 paragraph 2.

工会可以是匿名的;见标准,9.5 第 2 段。

What purpose do you see an anonymous struct or class as fulfilling? Before speculating why something isn't in the Standard, I'd like to have some idea why it should be, and I don't see a use for an anonymous struct.

您认为匿名结构或类的目的是什么?在推测为什么标准中没有某些东西之前,我想知道为什么它应该是,并且我没有看到匿名结构的用途。

回答by kennytm

Your code

你的代码

union {
  struct {
    float x;
    float y;
    float z;
  };
  float v[3];
};

is like

就好像

union Foo {
   int;
   float v[3];
};

which is surely invalid (in C99 and before).

这肯定是无效的(在 C99 及之前)。

The reason is probablyto simplify parsing (in C), because in that case you only need to check that the struct/union body has only "declarator statements" like

原因可能是为了简化解析(在 C 中),因为在这种情况下,您只需要检查结构/联合体是否只有“声明符语句”,例如

Type field;

That said, gcc and "other compilers"supports unnamed fields as an extension.

也就是说,gcc 和“其他编译器”支持未命名字段作为扩展。

Edit:Anonymous structs are now officially supported in C11 (§6.7.2.1/13).

编辑:匿名结构现在在 C11(第 6.7.2.1/13 节)中得到正式支持。

回答by JonM

Based on the edit, the comments, and this MSDN article: Anonymous Structures, I'll hazard a guess - it fits poorly with the concept of encapsulation. I wouldn't expect a member of a class to mess with my class namespace beyond merely adding one member. Furthermore, changes to the anonymous structure can affect my class without permission.

基于编辑、评论和这篇 MSDN 文章:匿名结构,我会冒险猜测 - 它与封装的概念不太吻合。除了仅仅添加一个成员之外,我不希望类的成员弄乱我的类命名空间。此外,匿名结构的更改可能会在未经许可的情况下影响我的班级。