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

Why does C++ disallow anonymous structs?(为什么 C++ 不允许匿名结构?)

本文介绍了为什么 C++ 不允许匿名结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

以下是我所说的示例:

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

我的编译器会接受这个,但它警告 "nameless struct/union" 是 C++ 的非标准扩展.

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

推荐答案

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

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

这样做的原因是 C 支持匿名联合而不是匿名结构*,因此 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++ 中匿名结构没有多大用处.你演示的使用,有一个包含三个浮点数的结构,可以被 .v[i].x, .y.z,我相信在 C++ 中会导致未定义的行为.C++ 不允许您写入联合的一个成员,例如 .v[1],然后从另一个成员读取,例如 .y.尽管执行此操作的代码并不少见,但实际上并没有很好地定义.

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++ 工具提供了替代解决方案.例如:

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

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 显然添加了匿名结构,因此 C++ 的未来修订版可能会添加它们.

这篇关于为什么 C++ 不允许匿名结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么 C++ 不允许匿名结构?

基础教程推荐