小编Mat*_*zak的帖子

C ++ 20中的指定初始值设定项

我对c ++ 20功能之一(指定的初始化程序)有疑问(有关此功能的更多信息,请点击此处

#include <iostream>

constexpr unsigned DEFAULT_SALARY {10000};

struct Person
{
    std::string name{};
    std::string surname{};
    unsigned age{};
};

struct Employee : Person
{
    unsigned salary{DEFAULT_SALARY};
};

int main()
{
    std::cout << std::boolalpha << std::is_aggregate_v<Person> << '\n'; // true is printed
    std::cout << std::boolalpha << std::is_aggregate_v<Employee> << '\n'; // true is printed

    Person p{.name{"John"}, .surname{"Wick"}, .age{40}}; // it's ok
    Employee e1{.name{"John"}, .surname{"Wick"}, .age{40}, .salary{50000}}; // doesn't compile, WHY ?

    // For e2 compiler prints a warning "missing initializer …
Run Code Online (Sandbox Code Playgroud)

c++ aggregate designated-initializer c++20

20
推荐指数
2
解决办法
442
查看次数

标签 统计

aggregate ×1

c++ ×1

c++20 ×1

designated-initializer ×1