Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++: Cannot use designated initializers on extended structs/classes

I am trying to figure out a way to use designated initializers to build a struct, which has been extended off of another one. In my use case, the struct S is a domain object, and the struct S2 adds some application-specific logic for converting it to/from json. As far as I can tell, you cannot use designated initializers if you have a "real" constructor, but all is well if you have a static method returning a new instance for you.

I have a MVP like so (view in Godbolt):

#include <iostream>

struct S {
    int i;
};

struct S2 : public S {
    static S2 create() {
        return S2 { .i = 1234 };
    }
};

int main() {
    std::cout << S2::create().i;
}

When building (using -std=c++20), I get the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<source>:9:31: error: 'S2' has no non-static data member named 'i'
    9 |         return S2 { .i = 1234 };
      |                               ^

I expect that the i field would be carried over and initialized, but it isn’t.

Am I trying to do something C++ cannot support?

>Solution :

i is data member of S, but not S2.

You can add another braces referring to the base subobject, e.g.

return S2 { {.i = 1234} };

Or you can just take advantage of brace elision:

return S2 { 1234 };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading