为什么指定的初始值设定项没有在 g++ 中实现

发布于 2024-10-16 04:58:26 字数 95 浏览 4 评论 0原文

g++ 中没有添加对指定初始值设定项的支持有什么具体原因吗?是因为C99标准来得晚,g++发展得更早,后来人们不关心这个问题,还是C++语法中实现指定初始化器有一些固有的困难?

Is there any specific reason why has support for designated initializers not been added to g++? Is the reason that C99 standards came late and g++ was developed earlier and later people didn't care about this issue, or there is some inherent difficulty in implementing designated initializers in the grammar of C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

牵你的手,一向走下去 2024-10-23 04:58:26

我今天遇到了同样的问题。 g++ 与 -std=c++11 和 c++14 确实支持指定的初始值设定项,但如果您不要按照定义结构成员的顺序初始化该结构。举个例子

struct x
{
    int a;
    int b;
};

// This is correct
struct x x_1 = {.a = 1, .b = 2};
// This will fail to compile with error non-trivial designated initializer
struct x x_2 = {.b = 1, .a = 2};

I ran into this same problem today. g++ with -std=c++11 and c++14 does support designated initializers, but you can still get a compilation error "test.cxx:78:9: sorry, unimplemented: non-trivial designated initializers not supported" if you don't initialize the struct in the order in which it's members have been defined. As an example

struct x
{
    int a;
    int b;
};

// This is correct
struct x x_1 = {.a = 1, .b = 2};
// This will fail to compile with error non-trivial designated initializer
struct x x_2 = {.b = 1, .a = 2};
放赐 2024-10-23 04:58:26

正如我在评论中指出的,G++ 不支持 C99 标准指定初始化程序,但它确实支持 C90 的 GNU 扩展,允许指定初始化程序。所以这行不通:

union value_t {
    char * v_cp;
    float v_f;
};
union value_t my_val = { .v_f = 3.5f };

但这确实行得通:

union value_t my_val = { v_f: 3.5f };

这似乎是 C 和 C++ 标准委员会之间协调的不良互动(C++ 没有特别充分的理由为什么不支持 C99 语法,他们只是没有这样做)没有考虑过)和 GCC 政治(C++ 不应该仅仅因为它是 C99 语法就支持 C99 语法,但它应该支持实现完全相同的事情的 GNU 扩展语法,因为这是一个可以应用于任何一种语言的 GNU 扩展)。

As I noted in a comment, G++ doesn't support C99 standard designated initialisers, but it does support the GNU extension to C90 which allows designated initialisers. So this doesn't work:

union value_t {
    char * v_cp;
    float v_f;
};
union value_t my_val = { .v_f = 3.5f };

But this does:

union value_t my_val = { v_f: 3.5f };

This seems to be a bad interaction of co-ordination between the C and C++ standards committees (there is no particularly good reason why C++ doesn't support the C99 syntax, they just haven't considered it) and GCC politics (C++ shouldn't support C99 syntax just because it's in C99, but it should support GNU extension syntax that achieves exactly the same thing because that's a GNU extension that can be applied to either language).

浮萍、无处依 2024-10-23 04:58:26

C++ 不支持这一点。它甚至不在 C++0x 标准中: http://groups.google.com/group/comp.std.c++/browse_thread/thread/8b7331b0879045ad?pli=1

另外,为什么要尝试使用 G++ 编译 Linux 内核?

C++ does not support this. It will not even be in the C++0x standards it seems: http://groups.google.com/group/comp.std.c++/browse_thread/thread/8b7331b0879045ad?pli=1

Also, why are you trying to compile the Linux kernel with G++?

梦醒灬来后我 2024-10-23 04:58:26

在 C++20 中得到正式支持,并且已在 g++8.2 中实现(即使没有 std=c++2a 标志)。

It is officially supported in C++20, and is already implemented in g++8.2 (even without the std=c++2a flag).

信愁 2024-10-23 04:58:26

至少从 g++-4.8 开始,现在默认支持此功能。

As of at least g++-4.8 this is now supported by default.

脱离于你 2024-10-23 04:58:26

根据
http://gcc.gnu.org/c99status.html
指定的初始化程序已经实现。

你使用什么版本的 g++? (尝试 g++ -- 版本)

Accoding to
http://gcc.gnu.org/c99status.html
designated initializers have been already implemented.

What version of g++ do you use? (Try g++ -- version)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文