Why does C++ disallow anonymous structs?(为什么 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引用code> 和
.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++ 不允许匿名结构?
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01