是“匿名结构”标准?而且,真的,什么*是*他们? [英] Are "anonymous structs" standard? And, really, what *are* they?

查看:121
本文介绍了是“匿名结构”标准?而且,真的,什么*是*他们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN 认为匿名结构在C ++中是非标准的:


Microsoft C扩展允许您在另一个结构中声明一个结构变量
而不给它一个名称。这些嵌套的
结构称为匿名结构。 C ++不允许
匿名结构。



您可以访问匿名结构的成员,就好像它们是包含结构中的
成员。 / p>

@ K-ballo 同意



我被告知此功能必须与创建未命名的结构体相同,但是我不能看到标准措辞方面的区别。



C ++ 11说:


[C ++ 11:9/1] [..] class-head 省略 class-head-name 定义未命名类别的类别


并为缺少名称的类型定义提供了完整的语法结构。



C ++ 03缺少此但类似地表示在类型定义中的标识符是可选的,并且引用 9.4.2 / 5中的未命名类 / code>和 3.5 / 4




  • ,这些东西都是完全标准的?

  • 或者有一些细微的未命名的结构/类之间缺少,同样的,当用作成员,防止它们被覆盖这个C ++ 03 / C ++ 11功能?

  • 我缺少未命名结构和匿名结构之间的一些根本区别?
  • 所有的标准文字都指的是创建一个未命名的 struct:

      struct {
    int hi;
    int bye;
    };

    只是一个很好的友好类型,没有可访问的名称。



    以标准方式,它可以被实例化为如下的成员:

      struct Foo {
    struct {
    int hi;
    int bye;
    } bar;
    };

    int main()
    {
    Foo f;
    f.bar.hi = 3;
    }






    是微妙的不同—它是一个未命名的结构体的组合和你奇迹般地得到成员在父对象中的事实:

      struct Foo {
    struct {
    int hi;
    int bye;
    }; //< ---没有会员名!
    };

    int main()
    {
    Foo f;
    f.hi = 3;
    }

    与直觉相反只创建一个嵌套在 Foo 中的未命名结构,但也会自动给你一个匿名成员,使成员可以在父对象中访问。



    这是非标准的功能。 GCC 支持它,Visual C ++也是如此。 Windows API标题默认使用此功能,但您可以在包含Windows头文件之前通过添加 #define NONAMELESSUNION 来指定不需要它。



    与匿名联盟的标准功能进行比较,它们做了类似的事情:

      struct Foo {
    union {
    int hi;
    int bye;
    }; //< ---没有会员名!
    };

    int main()
    {
    Foo f;
    f.hi = 3;
    }






    看来,虽然术语未命名是指类型(即类或结构)本身,术语匿名指的是实际的实例化成员(使用较旧的意义struct更靠近 struct y type的对象)。这可能是您初始混淆的根源。


    MSDN reckons that anonymous structs are non-standard in C++:

    A Microsoft C extension allows you to declare a structure variable within another structure without giving it a name. These nested structures are called anonymous structures. C++ does not allow anonymous structures.

    You can access the members of an anonymous structure as if they were members in the containing structure.

    @K-ballo agrees.

    I'm told that this feature isn't necessarily the same as just creating an unnamed struct but I can't see a distinction in terms of standard wording.

    C++11 says:

    [C++11: 9/1]: [..] A class-specifier whose class-head omits the class-head-name defines an unnamed class.

    and provides an entire grammatical construction for a type definition missing a name.

    C++03 lacks this explicit wording, but similarly indicates that the identifier in a type definition is optional, and makes reference to "unnamed classes" in 9.4.2/5 and 3.5/4.

    • So is MSDN wrong, and these things are all completely standard?
    • Or is there some subtlety I'm missing between "unnamed structs/classes" and the same when used as members that prevents them from being covered by this C++03/C++11 functionality?
    • Am I missing some fundamental difference between "unnamed struct" and "anonymous struct"? They look like synonyms to me.

    解决方案

    All the standard text refers to creating an "unnamed struct":

    struct {
       int hi;
       int bye;
    };
    

    Just a nice friendly type, with no accessible name.

    In a standard way, it could be instantiated as a member like this:

    struct Foo {
       struct {
          int hi;
          int bye;
       } bar;
    };
    
    int main()
    {
       Foo f;
       f.bar.hi = 3;
    }
    


    But an "anonymous struct" is subtly different — it's the combination of an "unnamed struct" and the fact that you magically get members out of it in the parent object:

    struct Foo {
       struct {
          int hi;
          int bye;
       }; // <--- no member name!
    };
    
    int main()
    {
       Foo f;
       f.hi = 3;
    }
    

    Converse to intuition, this does not merely create an unnamed struct that's nested witin Foo, but also automatically gives you an "anonymous member" of sorts which makes the members accessible within the parent object.

    It is this functionality that is non-standard. GCC does support it, and so does Visual C++. Windows API headers make use of this feature by default, but you can specify that you don't want it by adding #define NONAMELESSUNION before including the Windows header files.

    Compare with the standard functionality of "anonymous unions" which do a similar thing:

    struct Foo {
       union {
          int hi;
          int bye;
       }; // <--- no member name!
    };
    
    int main()
    {
       Foo f;
       f.hi = 3;
    }
    


    It appears that, though the term "unnamed" refers to the type (i.e. "the class" or "the struct") itself, the term "anonymous" refers instead to the actual instantiated member (using an older meaning of "the struct" that's closer to "an object of some structy type"). This was likely the root of your initial confusion.

    这篇关于是“匿名结构”标准?而且,真的,什么*是*他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆