<bdo id='PReeZ'></bdo><ul id='PReeZ'></ul>

    <i id='PReeZ'><tr id='PReeZ'><dt id='PReeZ'><q id='PReeZ'><span id='PReeZ'><b id='PReeZ'><form id='PReeZ'><ins id='PReeZ'></ins><ul id='PReeZ'></ul><sub id='PReeZ'></sub></form><legend id='PReeZ'></legend><bdo id='PReeZ'><pre id='PReeZ'><center id='PReeZ'></center></pre></bdo></b><th id='PReeZ'></th></span></q></dt></tr></i><div id='PReeZ'><tfoot id='PReeZ'></tfoot><dl id='PReeZ'><fieldset id='PReeZ'></fieldset></dl></div>
    <legend id='PReeZ'><style id='PReeZ'><dir id='PReeZ'><q id='PReeZ'></q></dir></style></legend>

      <small id='PReeZ'></small><noframes id='PReeZ'>

    1. <tfoot id='PReeZ'></tfoot>

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

      Why does C++ disallow anonymous structs?(为什么 C++ 不允许匿名结构?)
        <tfoot id='pdM8Y'></tfoot>

        <small id='pdM8Y'></small><noframes id='pdM8Y'>

        <legend id='pdM8Y'><style id='pdM8Y'><dir id='pdM8Y'><q id='pdM8Y'></q></dir></style></legend>

            <tbody id='pdM8Y'></tbody>
              <bdo id='pdM8Y'></bdo><ul id='pdM8Y'></ul>
            • <i id='pdM8Y'><tr id='pdM8Y'><dt id='pdM8Y'><q id='pdM8Y'><span id='pdM8Y'><b id='pdM8Y'><form id='pdM8Y'><ins id='pdM8Y'></ins><ul id='pdM8Y'></ul><sub id='pdM8Y'></sub></form><legend id='pdM8Y'></legend><bdo id='pdM8Y'><pre id='pdM8Y'><center id='pdM8Y'></center></pre></bdo></b><th id='pdM8Y'></th></span></q></dt></tr></i><div id='pdM8Y'><tfoot id='pdM8Y'></tfoot><dl id='pdM8Y'><fieldset id='pdM8Y'></fieldset></dl></div>
                本文介绍了为什么 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++ 不允许匿名结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                <legend id='ZwdLa'><style id='ZwdLa'><dir id='ZwdLa'><q id='ZwdLa'></q></dir></style></legend>

                  <tfoot id='ZwdLa'></tfoot>
                1. <i id='ZwdLa'><tr id='ZwdLa'><dt id='ZwdLa'><q id='ZwdLa'><span id='ZwdLa'><b id='ZwdLa'><form id='ZwdLa'><ins id='ZwdLa'></ins><ul id='ZwdLa'></ul><sub id='ZwdLa'></sub></form><legend id='ZwdLa'></legend><bdo id='ZwdLa'><pre id='ZwdLa'><center id='ZwdLa'></center></pre></bdo></b><th id='ZwdLa'></th></span></q></dt></tr></i><div id='ZwdLa'><tfoot id='ZwdLa'></tfoot><dl id='ZwdLa'><fieldset id='ZwdLa'></fieldset></dl></div>

                    <tbody id='ZwdLa'></tbody>
                2. <small id='ZwdLa'></small><noframes id='ZwdLa'>

                  • <bdo id='ZwdLa'></bdo><ul id='ZwdLa'></ul>