Linux 为什么指定的初始值设定项未在 g++ 中实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4900739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are designated initializers not implemented in g++
提问by Bharat
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++?
是否有任何特定原因未将指定初始化程序的支持添加到 g++ 中?是因为C99标准来得晚,g++开发得早,后来人们不关心这个问题,还是C++的语法中实现指定初始化器存在一些固有的困难?
采纳答案by Tom
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:
正如我在评论中指出的那样,G++ 不支持 C99 标准指定初始化程序,但它支持 C90 的 GNU 扩展,它允许指定初始化程序。所以这不起作用:
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).
这似乎是 C 和 C++ 标准委员会(没有特别好的理由为什么 C++ 不支持 C99 语法,他们只是没有考虑过)和 GCC 政治(C++ 应该t 支持 C99 语法只是因为它在 C99 中,但它应该支持实现完全相同的事情的 GNU 扩展语法,因为这是可以应用于任一语言的 GNU 扩展)。
回答by Maister
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
C++ 不支持这个。它甚至不会出现在 C++0x 标准中: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++?
另外,你为什么要尝试用 G++ 编译 Linux 内核?
回答by Alexandr Priymak
Accoding to http://gcc.gnu.org/c99status.htmldesignated initializers have been already implemented.
根据 http://gcc.gnu.org/c99status.html指定的初始化程序已经实现。
What version of g++ do you use? (Try g++ -- version)
你用的是什么版本的g++?(试试 g++ -- 版本)
回答by Anirban Mandal
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
我今天遇到了同样的问题。带有 -std=c++11 和 c++14 的 g++ 确实支持指定初始值设定项,但是如果您仍然会收到编译错误“test.cxx:78:9: sorry, unimplemented: non-trivial specified initializers not supported”不要按照定义它的成员的顺序初始化结构。举个例子
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};
回答by Catskul
As of at least g++-4.8 this is now supported by default.
至少从 g++-4.8 开始,现在默认支持。
回答by Martin
What about anonymous unions?
匿名工会呢?
In C I can have this:
在 CI 中可以有这个:
struct vardir_entry {
const uint16_t id;
const uint8_t sub;
const char *name;
const uint8_t type;
const union {
struct vardir_lookup lookup;
struct vardir_min_max_conf minmax;
};
const union {
const struct vardir_value_target_const const_value;
const struct vardir_value_target value;
};
};
And initialized like this:
并像这样初始化:
static const struct vardir_entry _directory[]{
{ .id = 0xefef, .sub = 0, .name = "test", .type = VAR_UINT32, .minmax = { .min = 0, .max = 1000 }, .value = VARDIR_ENTRY_VALUE(struct obj, &obj, member) }
};
However under g++ even with c++14 this gives the same "sorry, unimplemented" error. We do need to be able to define C variables in C++ when we at least want to unit test C code with C++ test framework. The fact that such a valuable feature from C is not being supported is quite a shame.
然而,在 g++ 下,即使使用 c++14 这也会产生相同的“抱歉,未实现”错误。当我们至少想要使用 C++ 测试框架对 C 代码进行单元测试时,我们确实需要能够在 C++ 中定义 C 变量。不支持来自 C 的如此有价值的功能这一事实令人遗憾。
回答by Elazar
It will be officially supported in C++20, and is already implemented in g++8.2(even without the std=c++2a
flag).
它将在 C++20 中得到官方支持,并且已经在 g++8.2 中实现(即使没有std=c++2a
标志)。