简体   繁体   English

为什么C ++ 11不支持匿名结构,而C11呢?

[英]Why does C++11 not support anonymous structs, while C11 does?

C11 supports anonymous structures, like so: C11支持匿名结构,如下所示:

struct Foo
{
    struct
    {
        size_t x, y;
    };
};
struct Foo f;
f.x = 17;
f.y = 42;

Basically, the members of such a struct are treated as if they were members of the enclosing struct or union (recursively, if the enclosing structure was itself anonymous). 基本上,这样的struct的成员被视为它们是封闭structunion成员(如果封闭结构本身是匿名的,则递归地)。

What was the rationale for C++11 not also including anonymous structures? C ++ 11的理由是什么,还包括匿名结构? They're only uncommonly useful (mostly inside unions, to eliminate the typing of an identifier for the struct ), certainly. 当然,它们只是非常有用(主要是在联合内部,以消除struct的标识符的类型)。 But they seem an obvious enough addition to the specification (and one already implemented by many compilers) that surely they must have been discussed, at the very least to preserve compatibility with the C11 standard. 但它们似乎是对规范的明显补充(以及已经由许多编译器实现的规范),它们肯定必须经过讨论,至少是为了保持与C11标准的兼容性。 So why weren't they added? 那他们为什么不加?

Little effort has been made to maintain compatibility between C++ and C as the two languages evolve. 随着两种语言的发展,在C ++和C之间保持兼容性的努力很少。 Notice that variable length stack arrays have been in C since 1999, but weren't included in C++11. 请注意,自1999年以来,可变长度堆栈数组已在C中,但未包含在C ++ 11中。 While they generally don't introduce things that contradict one another, the C++ committee isn't exactly bending over backwards to make sure that C++11 is compatible with versions of C beyond C89. 虽然它们通常不会引入相互矛盾的东西,但C ++委员会并没有完全反过来确保C ++ 11与C89之外的C版本兼容。

Furthermore, this feature would be quite complex in C++, because a struct is nothing more than a class . 此外,这个特性在C ++中会非常复杂,因为struct只不过是一个class And an anonymous struct/class should have all of the features of a regular struct/class, yes? 一个匿名的struct / class应该具有常规struct / class的所有功能,是吗? Otherwise, what's the point of having it? 否则,有什么意义呢?

What would it mean to construct a nameless struct ? 构建无名struct意味着什么? How would you define the constructor? 你会如何定义构造函数? Something as simple as: 简单的事情:

struct Foo
{
    struct
    {
        size_t &x;
    };
};

is simply not possible because the inner struct has no constructor. 根本不可能,因为内部struct没有构造函数。 And there's no way to specify one. 并且没有办法指定一个。 A struct cannot construct the members of another struct within it. struct不能构造其中的另一个struct的成员。

For something like this: 对于这样的事情:

struct Foo
{
    size_t outer;
    struct
    {
        void SomeFunc();
        size_t x;
    };
};

What this pointer does SomeFunc get? 什么this指针不SomeFunc得到什么? What would the type of this be, the nameless and unnamed type? 什么会的类型this是无名和无名类型? How would you even define SomeFunc outside of the struct? 你怎么会在结构之外定义SomeFunc The name of SomeFunc can't be Foo::SomeFunc , because SomeFunc lives in an inner scope. SomeFunc的名称不能是Foo::SomeFunc ,因为SomeFunc位于内部范围内。

It's just too complex for C++ to deal with. 这对C ++来说太复杂了。 And certainly not worthwhile enough to bother with adding that complexity for. 当然不值得为添加复杂性而烦恼。

To play devil's advocate - class and struct declarations are used often to wrap class-specific type declarations. 扮演魔鬼的拥护者 - 经常使用类和结构声明来包装特定于类的类型声明。

typedef struct {

} name;

therefore should be allowable. 因此应该是允许的。

Therefore 因此

struct {

} 

should be as well. 应该也是。

However, if we consider this as just a declaration within a class' internal namespace, there would be no way to access the inside of the struct. 但是,如果我们将此视为类内部命名空间中的声明,则无法访问结构内部。

Because struct != namespace in C, C can make up rules like accessing an anonymous struct through the surrounding struct. 因为C中的struct!= namespace,所以C可以构成通过周围结构访问匿名结构的规则。

For C++ to allow this it would need to special case this situation, which would complicate name resolution. 对于C ++来说,允许这种情况需要特殊情况,这会使名称解析变得复杂。

Of course, playing devil's devil's advocate - C actually did this. 当然,扮演魔鬼的魔鬼的拥护者--C实际上是这样做的。 It added an extra level to name resolution - if you can't find the name in a stuct check the struct's anonymous members. 它为名称解析添加了额外的级别 - 如果您在stuct中找不到该名称,请检查该结构的匿名成员。 Which is a little magical, in a way that I can see C++ committee members finding annoying. 这有点神奇,在某种程度上我可以看到C ++委员会成员发现很烦人。

It also raises questions - if an anonymous struct can be accessed through its parent class, what about anonymous structs in a namespace. 它还提出了一些问题 - 如果可以通过其父类访问匿名结构,那么名称空间中的匿名结构如何。

Of course, if you really want to know, just ask Stroustrup - he responds to emails. 当然,如果你真的想知道,只要问Stroustrup - 他会回复电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM