Designated initializers in C++20

后端 未结 2 1516
我在风中等你
我在风中等你 2020-12-20 11:02

I\'ve got a question about one of the c++20 feature, designated initializers (more info about this feature here)

#include 

constexpr unsigne         


        
2条回答
  •  无人及你
    2020-12-20 11:45

    You might have several fields with same name from different bases,

    so logically, you should provide name of the wanted base, but it seems there is no way to do it.

    // Invalid too:
    Employee e1{.Person.name{"John"}, .Person.surname{"Wick"}, .Person.age{40}, .salary{50000}};
    Employee e2{.Person{.name{"John"}, .surname{"Wick"}, .age{40}}, .salary{50000}};
    

    In addition C++ designated initialization is more constrained than C:

    Note: out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++.

提交回复
热议问题