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++?
-
Linux is written in C and not C++. g++ is the frontend for C++, use gcc for C. – Jens Gustedt Feb 04 '11 at 17:13
-
g++ is a C++ compiler. Use gcc. – bobbogo Feb 04 '11 at 17:15
-
We have re written some portion of linux kernel in C++, so it is essential for us to use g++ – Bharat Feb 04 '11 at 17:31
-
1@Bharat Singh: Using C++ in the Linux kernel is a very, very bad idea. Linux lacks all the infrastructure required to make C++ work properly. Linus Torvalds explained this multiple times in detail: http://kerneltrap.org/node/2067 – datenwolf Feb 04 '11 at 17:37
-
1We have an implementation of the linux OS in C++, with the core kernel in C with some external wrapper functions providing support for the OO external hardware drivers. Just tell me is it possible to modify gcc so that it supports designated initializers in C++ – Bharat Feb 04 '11 at 18:01
-
2Compile C files with a C compile and C++ files with a C++ compiler. Designated initializers work perfectly well in C but they are not valid in C++. – R.. GitHub STOP HELPING ICE Feb 04 '11 at 18:48
-
3I want to know the reason why are they not supported by C++ – Bharat Feb 05 '11 at 04:51
-
2Why don't you just compile the C parts of the kernel with the C compiler, your C++ parts with `g++`, then link together the result? – caf Feb 07 '11 at 02:00
7 Answers
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};
- 161
- 1
- 8
-
1Designated initializers still aren't part of any version of standard C++. – Quentin Jul 22 '16 at 12:47
-
-
@Qu9entin: Designated initializers are part of C++20. So yes, finally, they are arriving. – Kai Petzke Sep 21 '20 at 10:02
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).
- 6,617
- 1
- 36
- 59
-
2this is no longer true in g++-5.2 and probably going all the way back at least to g++-4.8 – Catskul Oct 07 '16 at 20:00
-
1
-
2Yep, sorry, that was meant as a note to readers rather than a correction per se. – Catskul Oct 18 '16 at 19:30
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++?
- 4,872
- 1
- 30
- 32
-
4We have re written some portion of linux kernel in C++, so it is essential for us to use g++ – Bharat Feb 04 '11 at 17:17
-
14
It is officially supported in C++20, and is already implemented in g++8.2 (even without the std=c++2a flag).
- 18,541
- 4
- 43
- 67
What about anonymous unions?
In C I can have this:
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.
- 3,332
- 3
- 23
- 29
Accoding to http://gcc.gnu.org/c99status.html designated initializers have been already implemented.
What version of g++ do you use? (Try g++ -- version)
- 1,523
- 9
- 10
-
3That's C99, not C++. I don't think every C99 feature is implicitly supported by g++. – Maister Feb 04 '11 at 17:13
-
-
The poster wants C99 constructs in C++, which is completely wrong. – Conrad Meyer Feb 05 '11 at 21:06
-
5i don't want C99 constructs, i want to know the reason why they are not supported in C++, if there is some valid reason why they are not supported, there would me no point for me spending time in changing gcc. – Bharat Feb 06 '11 at 05:40
-
Put very simply, C and C++ are two different languages with different standards. Mostly the C++ standard includes everything in the C standard, but designated initialisers is one thing that is in the C99 standard but not any of the C++ standards. So G++ correctly implements the C++ standard by not including designated initialisers. – Tom Jan 30 '12 at 11:01
-
But note that G++ **does** support the GNU C extension - use `{ m_f: 3.5f}` instead of `{ .m_f = 3.5f }`. It's not portable, but it works in G++. – Tom Jan 30 '12 at 11:02