0

Let's say I have following declaration in the C++:

struct Configuration {

 struct ParametersSetA {
    float param_A_01;
    float param_A_02;
        float param_A_03;
 } parameters_set_A;

 struct ParametersSetB {
    float param_B_01;
    float param_B_02;
 } parameters_set_B;

};

Then somewhere in the code I have following definition:

Configuration config = {
 
 .parameters_set_A = {
  .param_A_01 = 1.0f,
  .param_A_02 = 2.0f, 
  .param_A_03 = param_A_01 + param_A_02;
  },

 .parameters_set_B = {
  .param_B_01 = 0.50f,
  .param_B_02 = 0.75f
  }
};

My question is whether the initialization (especially as far as the param_A_03 item in the nested struct ParametersSetA) I have used above is correct in the C++?

6
  • 1
    Does it compile? Commented Oct 17, 2022 at 11:36
  • 1
    It is not going to compile, so I guess the obvious answer is no. But it is easy to fix: param_A_01 -> config.parameters_set_A.param_A_01 and similar for the other. And the semicolon after that line doesn't belong there either. Commented Oct 17, 2022 at 11:36
  • Related: Nested designated initializers Commented Oct 17, 2022 at 11:36
  • The problem is that you can't use param_A_01 + param_A_02 when initializing param_A_03. Commented Oct 17, 2022 at 11:40
  • Designated initializes are available since C++20, so please add respective tag if this limitation is intentional. Commented Oct 17, 2022 at 11:53

1 Answer 1

3

The problem is that we can't use any of the unqualified names param_A_01 or param_A_02 or an expression that involves any of the two like param_A_01 + param_A_02 as initializer forparam_A_03.

Additionally, you have incorrectly put a semicolon ; after param_A_01 + param_A_02 instead of comma , . I've corrected both of these in the below shown modified program:

Method 1

//create constexpr variables 
constexpr float A1 = 1.0f;
constexpr float A2 = 2.0f;
Configuration config = {
 
 .parameters_set_A = {
  .param_A_01 = A1,
  .param_A_02 = A2, 
//--------------vv---vv---->use those constexpr variable(also semicolon is removed from here)
  .param_A_03 = A1 + A2
  },

 .parameters_set_B = {
  .param_B_01 = 0.50f,
  .param_B_02 = 0.75f
  }
};

Working demo

Method 2

Other option is to use qualified names like config.parameters_set_A.param_A_01 and config.parameters_set_A.param_A_02 as shown below:

Configuration config = {
 
 .parameters_set_A = {
  .param_A_01 = 1.0f,
  .param_A_02 = 2.0f, 
//--------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv--->qualified name used here
  .param_A_03 = config.parameters_set_A.param_A_01 + config.parameters_set_A.param_A_02 
  },

 .parameters_set_B = {
  .param_B_01 = 0.50f,
  .param_B_02 = 0.75f
  }
};

Working demo

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for your answer. My original intention was to calculate the initialization value for the param_A_03 based on the values of param_A_01 and param_A_02 at compile time. Do you know how to do that?
@Steve One option is to create constexpr variables for param_A_01 and param_A_02 called A1 and A2 respectively and then use those. See Demo. You're welcome.
I see, thanks. Let's say I would like to avoid the constexpr variables. Is it ok to use the variant mentioned above by the user17732522?
@Steve Yes, you can use the qualified names config.parameters_set_A.param_A_01 and config.parameters_set_A.param_A_02. I've added that as method 2 in my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.