0

I have a designated initializer list with multiple levels from C-Code like the following and I want to transform the whole codebase to C++.

C-Code:

wifi_config_t wifi_config = {
    .sta = {
        .ssid = EXAMPLE_ESP_WIFI_SSID,
        .password = EXAMPLE_ESP_WIFI_PASS,
        .threshold.authmode = WIFI_AUTH_WPA2_PSK,
        .pmf_cfg = {
            .capable = true,
            .required = false
        },
    },
};

How do I transform it to C++ so that the compiler understands it? I have a lot of setups like that so I would appreciate a simple and fast solution.

glades
  • 3,778
  • 1
  • 12
  • 34
  • 3
    If `wifi_config_t` is a C compatible struct (i.e. no C++ virtual functions, constructors, etc.), then the simple solution is to compile the designated initializers in straight C and use `extern` in the C++ code. (e.g.) Just create some `.c` files that _only_ have the initializers. Up until the latest C++ standard, it did _not_ have designated initializers. And, the new standard requires that they be in the correct order (so, somewhat useless IMO). – Craig Estey Oct 29 '21 at 16:12
  • @CraigEstey: that is a solution but the point is I need to plug in translation-unit specific data into the config struct. Compare the preprocessor macros EXAMPLE_ESP_WIFI_SSID which is only given in said TU. I would have to externalise that as well and I don't want to because it hurts readability and modularity. – glades Nov 01 '21 at 07:00

0 Answers0