1

Sorry for being not specific, but...

Suppose I have this struct:

struct OptionData
{
    Desc desc;
    bool boolValue;
    std::string stringValue;
    int intValue;
    double numberValue;
};

which I'm using this way:

OptionData isWritePatchesOptionData = {isWritePatchesDesc, {}, {}, {}, {}};

As I have lots of those options, I'd like to do s.t. like this:

<type_here> OptionDataList = {{}, {}, {}, {}};

so I can do:

OptionData isSeamCutOptionData = {isSeamCutDesc, OptionDataList};

but really on the spot I can't figure what type_here would be... Or maybe is not possible in this form... I mean, without creating an OptionDataList object into the OptionData struct... but that clearly would be redundant...

1
  • 2
    why not use a constructor? Commented Mar 9, 2020 at 12:55

1 Answer 1

2

Just provide default initializers. Using

struct OptionData
{
    Desc desc{};
    bool boolValue{};
    std::string stringValue{};
    int intValue{};
    double numberValue{};
};

Everything in the struct will be value initialized meaning objects with a constructor will be default constructed and objects without a constructor will be zero initialized.

This lets you write

OptionData isWritePatchesOptionData = {isWritePatchesDesc}; // same as using {isWritePatchesDesc, {}, {}, {}, {}};
// and
OptionData isSeamCutOptionData = {isSeamCutDesc};

and now all of the other members are in a default/zero state.

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

2 Comments

I think this is the straighest. Also I could do: OptionData isWritePatchesOptionData = {isWritePatchesDesc, {}}; - but that probably result in indeterminated initialization for the struct members... but not sure.
@Kabu That would work as well. Everything will still be value initialized so default for constructor and zero for non-constructors.

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.