Features and Bug fixes in Initialization : C++ 20

Features and Bug fixes in Initialization : C++ 20

Designated Initialization:

Now individual aggregate elements can be initialized using below syntax.

Awesome!!! Isn’t it ???

struct Foo 
{
	int a;
	int b;
	int c;
};	
	
int main()
{
   Foo foo{.a=3, .b=4}'  // .b=0 by default
}

Array deduction:

Finally this bug related to array deduction is fixed in C+20

double a[]{1,2,3,4};  //OK
double *ptr = new double[]{1,2,3}; //Error : C++17 Ok: C++20

Aggregate no longer declare constructors:

struct Foo 
{
	Foo() = delete;
	int a;
	int b;
	int c;
};
	
Foo foo;  //Error
Foo foo{};  //OK:C++17, Error:C++20

Support for Aggregates initialization with parenthesis in C++20:

Foo foo(1,2);  //OK: C++20
int arr[3](0,1,2);  //OK: C++20

Conclusion:

() and {} work same except.
– () doesn’t call std::initializer_list constructor
– {} doesn’t support narrowing conversion