0

So my current code looks like the following:

static Item fields[] = 
{
{GROUP1, TEXT1},
{GROUP2, 0},
}

Now I need to make change in such a way that I initialize GROUP2 only if certain condition is met else need to initialize with GROUP3. So I tried the following:

static Item fields[] = (flagSet)?
{
{GROUP1, TEXT1},
{GROUP2, 0},
} : {
{GROUP1, TEXT1},
{GROUP3, 0},
}

But this didn't work. I know that one way is to use #ifdef macros but this flagSet happens at runtime and based on that I need to initialize the static array. Also since the static initialization happens before anything else is it possible to do this at all?

ZoomIn
  • 1,237
  • 1
  • 17
  • 35
  • If this is a global variable, the keyword `static` doesn't seems to be relevant to the question. Global variables are initialized before entering the function `main`, whenever they are static or not. – Piotr Siupa Nov 16 '21 at 08:58
  • Yeah, it is a global variable. Not part of any class or function. – ZoomIn Nov 16 '21 at 08:59
  • Is `flagSet` somehow known before entering the `main` or it is just a normal value calculated during the program's proper runtime. – Piotr Siupa Nov 16 '21 at 08:59
  • `flagSet ? Item{..} : Item{..};`? – Jarod42 Nov 16 '21 at 09:02
  • 2
    Quite unclear what you really need! If your flag is not a preprocessor value nor a compile time constant, but calculated at runtime, I have no idea in which time before main this should happen! The only gap in time that your flag is initialized before main and before your fields variable. Take care about the static initialization order fiasco! And even if it is a static global variable, you can modify the content also later. What is the real problem you want to solve? – Klaus Nov 16 '21 at 09:12
  • I don't get the part of the `flagSet` being set in runtime. If it is initialized after entering the `main` function, you cannot use it as condition to initialize a static variable. Maybe you want to rely on environmental variables instead? – pptaszni Nov 16 '21 at 09:22
  • @NO_NAME flagSet is initialized in App::InitInstance() which is similar to main in Win32 application. – ZoomIn Nov 16 '21 at 09:26
  • 1
    Global variables are initialized before anything else in the application, long before the value of `flagSet` is known. Your best bet is to initialize the array to some neutral value and edit it after `flagSet`, perhaps also in `App::InitInstance()`. – Piotr Siupa Nov 16 '21 at 09:34
  • 1
    Does it really need to be a global? Function-scope statics are initialized when the function is first called, so a trivial function `auto& getFields() { fields[] = ...; return fields; }` will delay the initialization until first use. – MSalters Nov 16 '21 at 09:57

1 Answers1

3

Is there a way to conditionally initialize a global static variable?

Yes. The ways are pretty much the same as conditionally initialising a non-global non-static variable.

You cannot however conditionally initialise an array. You could use a bit of indirection:

static Item fields_true[] {
    {GROUP1, TEXT1},
    {GROUP2, 0},
};

static Item fields_false[] = {
    {GROUP1, TEXT1},
    {GROUP3, 0},
};

static auto& fields =
      flagSet
    ? fields_true
    : fields_false;

Or, you could initialise the array elements conditionally. Since only one element has a difference, there isn't even any repetition in this case:

static Item fields[] = {
    {GROUP1, TEXT1},
    {flagSet ? GROUP2 : GROUP3, 0},
};

but this flagSet happens at runtime

Using runtime input is not an option to initialise static objects. You will have to modify the array after initiliasation using assignment operation.

eerorika
  • 232,697
  • 12
  • 197
  • 326