15

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++?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Bharat
  • 2,139
  • 2
  • 16
  • 35
  • 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
  • 2
    @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
  • 1
    We 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
  • 2
    Compile 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
  • 3
    I want to know the reason why are they not supported by C++ – Bharat Feb 05 '11 at 04:51
  • 2
    Why 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

6 Answers6

13

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};
Anirban Mandal
  • 161
  • 1
  • 8
11

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

Tom
  • 7,269
  • 1
  • 42
  • 69
9

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++?

Maister
  • 4,978
  • 1
  • 31
  • 34
4

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

Elazar
  • 20,415
  • 4
  • 46
  • 67
2

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

Catskul
  • 17,916
  • 15
  • 84
  • 113
-3

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

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

Oleksandr Pryimak
  • 1,561
  • 9
  • 11
  • 3
    That's C99, not C++. I don't think every C99 feature is implicitly supported by g++. – Maister Feb 04 '11 at 17:13
  • Maybe you do not use "C mode" – Oleksandr Pryimak 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
  • 5
    i 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