为什么指定的初始值设定项没有在 g++ 中实现
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我今天遇到了同样的问题。 g++ 与 -std=c++11 和 c++14 确实支持指定的初始值设定项,但如果您不要按照定义结构成员的顺序初始化该结构。举个例子
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
正如我在评论中指出的,G++ 不支持 C99 标准指定初始化程序,但它确实支持 C90 的 GNU 扩展,允许指定初始化程序。所以这行不通:
但这确实行得通:
这似乎是 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:
But this does:
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).
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++?
它在 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).至少从 g++-4.8 开始,现在默认支持此功能。
As of at least g++-4.8 this is now supported by default.
根据
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)