为什么C ++ 11不支持匿名结构,而C11呢? [英] Why does C++11 not support anonymous structs, while C11 does?

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

问题描述

C11支持匿名结构,像这样:

C11 supports anonymous structures, like so:

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).

什么是C ++ 11不是也包括匿名结构的原因是什么?他们只用不寻常的(主要是里面的工会,以消除对结构标识符的打字),肯定。但他们似乎明显足够除了规范(和已经由许多编译器实现的一个),该肯定他们必须已讨论的,至少是到preserve兼容性与C11的标准。那么,为什么不加入他们?

What was the rationale for C++11 not also including anonymous structures? They're only uncommonly useful (mostly inside unions, to eliminate the typing of an identifier for the struct), certainly. 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. So why weren't they added?

推荐答案

小已作出努力来维护和C C之间的兼容性++作为两种语言发展。请注意,从1999年开始变长栈阵列已经在C,但均未列入C ++ 11。虽然他们一般不引进的东西,相互矛盾时,C ++委员会是不完全弯腰向后,以确保C ++ 11是具有超越C89的C版本兼容。

Little effort has been made to maintain compatibility between C++ and C as the two languages evolve. Notice that variable length stack arrays have been in C since 1999, but weren't included in 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 ++中相当复杂,因为结构无非是多个类。而一个匿名结构/类应该有所有的常规结构/类的功能,是吗?否则,什么是有它的意义呢?

Furthermore, this feature would be quite complex in C++, because a struct is nothing more than a class. And an anonymous struct/class should have all of the features of a regular struct/class, yes? Otherwise, what's the point of having it?

意味着什么构建一个无名结构?你如何定义构造函数?事情就这么简单:

What would it mean to construct a nameless struct? How would you define the constructor? Something as simple as:

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

是根本不可能的,因为里面的结构没有构造函数。而且也没有办法指定一个。 A 结构不能建立在它的另一个结构成员。

is simply not possible because the inner struct has no constructor. And there's no way to specify one. A struct cannot construct the members of another struct within it.

这样的事情:

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

什么这个指针确实 SomeFunc 得到什么?会是什么的类型本是,无名和无名类型?你将如何界定,甚至 SomeFunc 的结构之外吗? SomeFunc 不能富:: SomeFunc ,因为 SomeFunc 生活在内部范围。

What this pointer does SomeFunc get? What would the type of this be, the nameless and unnamed type? How would you even define SomeFunc outside of the struct? The name of SomeFunc can't be Foo::SomeFunc, because SomeFunc lives in an inner scope.

这是对C太复杂++来处理。而且肯定不够用值得补充说,复杂性打扰。

It's just too complex for C++ to deal with. And certainly not worthwhile enough to bother with adding that complexity for.

这篇关于为什么C ++ 11不支持匿名结构,而C11呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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