C++20可以指派初始化器,可显示指明需初始化的成员变量,初始化部分成员变量。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct test
{
int n;
string str;
double db;
};
int main()
{
//c++11
test t{1,"hello",3.14};
cout<<"t:"<<t.n<<" "<<t.str<<" "<<t.db<<endl;
//c++20
test t2{.n = 10,.db = 3.14};
cout<<"t2:"<<t2.n<<" "<<t2.str<<" "<<t2.db<<endl;
return 0;
}