嵌入式C ++静态初始化结构体数组 [英] Embedded C++ static initialization of struct arrays

查看:251
本文介绍了嵌入式C ++静态初始化结构体数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在迁移到C ++时,我需要一个似乎已弃用的特定功能。

对不起,未实现:不支持非平凡的指定初始化器

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

在内存约束系统中用C ++实现以下数据存储系统的正确方法是什么?

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 },
    }
};

经常使用 std:map 或建议使用 std:vector 。这很合适,但是 list 是不可变的,必须能够编译并链接到特定的Flash块。

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.

我能去的最高的是ARM编译器6,它是C ++ 14。

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

推荐答案

您显示的方式几乎完全符合传入的C ++ 20标准。也只需初始化 .d

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

要在C ++ 14中处理此问题,您必须对其进行初始化:

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
    }
};

这篇关于嵌入式C ++静态初始化结构体数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆