简体   繁体   中英

Using initializer list for a struct with inheritance

I have a struct that I'd like to initialize using an initializer list

struct Parent{};

struct Child : private Parent {
    int b;
};

int main() {
    Child c{ 1 };
    return 0;
}

Looking at other questions I found this answer that states that it should be possible in c++17.

However when I try the above snippet in VS2017 I get

Error   C2440   'initializing': cannot convert from 'initializer list' to 'child'

Is there any way to utilize this new feature?

With inheritance, each base class subobject of the aggregate is initialized like a member. So to aggregate initialization, Child has two subobjects: Parent , and i . So you need two initializers in your braced-init-list:

Child c{ {}, 1 };

Also, in order for Child to be an aggregate, all subobjects must be public . So you can't have private base classes.

Of course, this assumes that Visual Studio implements the feature correctly. VS2017 15.5 is not C++17 compliant, but 15.7 supports this.

has introduced an Extension to Aggregate Initialization(P0017R1) which provides for the construction of a derived instance while still explicitly initializing the base class:

struct base { int a1, a2; };
struct derived : base { int b1; };

derived d1{{1, 2}, 3};      // full explicit initialization
derived d1{{}, 1};          // the base is value initialized

Thus using the "Extension to Aggregate Initialization" you'll want to use the code: Child c{ {}, 1 } as mentioned in Nicol Bolas's answer again with his caveat that you'd need to use public not private inheritance to do aggregate initialization at all.

Unfortunately did not support P0017R1 until version 15.7 . So it may be necessary to upgrade your Visual Studio to accomplish this.


If that's not possible, and you're able to get by without polymorphisim, you can temporarily define:

struct Child {
    Parent a;
    int b;
};

Which would allow you to use the consistent code: Child c{ {}, 1 } now and whenever you change back to inheritance after upgrading to 15.7.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM