This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TM4C1231H6PM: Using designated initializers with C++ build (C++20?)

Part Number: TM4C1231H6PM

Tool/software: TI C/C++ Compiler

I am trying to port a project from C to C++. At least during the transition, I would like to be able to compile it in either mode so I can compare performance, memory usage, etc. so I am trying to make the code use a common subset of features that are supported in both languages.  Currently the code uses designated initializers in a lot of places, which work great in C and won't compile in C++ mode (I get error "#2719: a designator for an anonymous union member can only appear within braces corresponding to that anonymous union").  I think that is most likely because the TI v20.2.0.LTS compiler I am using (which came with CCS10) supports only C++14, not C++20.  I realize that C++20's support for designated initializers is limited compared with C++11, but I think I could deal with that pretty easily.  Do I have any good options for making this code compile in both C and C++ modes?

  • Is there a way to make the TI v20.2.0.LTS compiler support designated initializers in C++ mode?
  • Is there a newer version of that compiler that supports C++ 20, or at least a schedule for when one might be available?
  • Would switching to the GNU v7.2.1 (Linaro) compiler help?
  • Is there another way to handle initializers that works in both C and C++ (other than the original non-designated method which just relies on the order the members were defined)?

Thanks for any suggestions.

Steve

  • Steve Strobel said:
    Is there a newer version of that compiler that supports C++ 20, or at least a schedule for when one might be available?

    Unfortunately, no.

    Steve Strobel said:
    Would switching to the GNU v7.2.1 (Linaro) compiler help?

    I was unable to find a way to make it work with this compiler.

    Steve Strobel said:
    Is there another way to handle initializers that works in both C and C++

    I found a solution for you to consider.  This only works with the TI ARM compiler.

    I presume your C code is similar to this example ...

    struct outer {
       int i1;
       union
       {
          int    u1;
          long   u2;
          double u3;
       }; /* anonymous */
    };
    
    struct outer outer_instance = {
       .u1 = 2,
       .i1 = 1
    };

    This compiles as C code.  But when compiled as C++, this diagnostic issued ...

    Steve Strobel said:
    #2719: a designator for an anonymous union member can only appear within braces corresponding to that anonymous union

    When I change it to ...

    struct outer {
       int i1;
       union
       {
          int    u1;
          long   u2;
          double u3;
       } union_member;
    };
    
    struct outer outer_instance = {
       .union_member = { .u1 = 2 },
       .i1 = 1
    };

    ... it compiles as both C code and C++ code.  Note the lines with union_member on them are different.  All other lines are the same.

    Thanks and regards,

    -George

  • Thanks, George, for the reply.  At first I misunderstood what was significant about the change you showed and thought that the key was wrapping the structure members in unions (I used unions for each one since they needed to be stored in separate memory locations).  When compiled as C, that gave me exactly the same binary image as without the "union" wrappers.  It also compiled as C++ (I still couldn't link because of other errors).  

    I then ran into some bit fields, and figured that wrapping them in individual unions was going to prevent them from packing correctly.  So I dug a little deeper and figured out that the key was avoiding anonymous structs or unions, rather than wrapping members in unions.  So, as in your example, I added names to nested structures and unions (so they were no longer anonymous) and inserted that name in the references to them (x->union_member.u1).  That seems to have solved my problem.  I don't know if it is standard C++11 or not, but it works in CSS which is all that really matters in this application (I think it is probably valid C and C++20).

    Thanks again,
    Steve