1

While migrating to C++ I require a certain function that seems to have been deprecated.
sorry, unimplemented: non-trivial designated initializers not supported

What is the correct way to implement the following data storage system in C++ for memory constraint systems?

typedef union union_t {
    float f;
    int i;
} arg;

typedef struct type_t {
    int a;
    arg b;
    int d;
} element;

const element list[] = {
    {
      .a = 1,
      .b = { .f = 3.141519f },
      .d = 6
    },
    {
      .a = 3,
      .b = { .i = 1 },
    }
};

Often the use of std:map or std:vector is suggested. Which is suitable, however list is immutable and must be able to compile and link to a specific block of flash. Both seem unfit for that purpose.

The highest I can go is ARM Compiler 6, which is C++14.

3
  • 2
    this is a new feature coming in C++20, it's not "deprecated" (which means an old feature that is going to be removed) Commented Oct 25, 2019 at 13:35
  • 2
    For now, you need to use standard C in order to use designated initializers. Also, creating "variant" type unions is a very bad idea in either language. I can't think of any reason why you would use union in C++. Commented Oct 25, 2019 at 13:38
  • 1
    And no, std::map and std::vector are not suitable since this is an embedded system, not a PC. Use std::array or POD types. "...compile and link to a specific block of flash. Both seem unfit for that purpose." The whole C++ language is unfit for that purpose. Commented Oct 25, 2019 at 13:40

1 Answer 1

2

The way you shown is almost correct compliant with the incoming C++20 standard. Only that .d also have to be initialized. Is it what I suggest to use.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0329r4.pdf

To handle this in C++14 you have to initilize it explicilty:

typedef union union_t {
    float f;
    int i;
} arg;

typedef struct type_t {
    int a;
    arg b;
    int d;
} element;

const element list[] = {
    {
      /*.a = */ 1,
      /*.b = */ { /*.f = */ 3.141519f },
      /*.d = */ 6
    },
    {
      /* .a = */ 3,
      /* .b = */ { /* .i = */ 1 },
      0
    }
};
Sign up to request clarification or add additional context in comments.

6 Comments

I don't find the requirement for explicitly initializing .d in there (see the very first example). d would be initialized with {} if missing.
Unfortunately ARM Compiler 6 does not support higher than C++14
In your example, how do I specify which union field is being initialized?
Compiler will do it, your task is to provide correct type
@majkrzak the warning of "incorrect type" is not going away is it? (c++ errors on it btw)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.