Is direct-initialization equivalent to direct-list-initialization?

252 views Asked by At

I have the following example:

struct S{
   int x, y;
}

S s1{1};    // direct-initialization or direct-list-initialization ?
S s2{1, 2}; // direct-initialization or direct-list-initialization ?
S s3(1);    // direct-initialization or direct-list-initialization ?
S s4(1, 2); // direct-initialization or direct-list-initialization ?
int i1{10}; // direct-initialization or direct-list-initialization ?
int i2(10); // direct-initialization or direct-list-initialization ?

My questions

  • I just need to know what's the type of initialization in the above statements?
  • Is there any standard quote applied here?

I am already checked the question direct-initialization vs direct-list-initialization (C++), but it hasn't strict answer yet.

1

There are 1 answers

2
user12002570 On BEST ANSWER

From direct initialization's documentation:

T object ( arg );

T object ( arg1, arg2, ... ); (1)

T object { arg }; (2) (since C++11)

T ( other )

T ( arg1, arg2, ... ) (3)

Direct initialization is performed in the following situations:

    1. initialization with a nonempty parenthesized list of expressions or braced-init-lists (since C++11)
    1. initialization of an object of non-class type with a single brace-enclosed initializer (note: for class types and other uses of braced-init-list, see list-initialization)
    1. initialization of a prvalue temporary (until C++17)the result object of a prvalue (since C++17) by functional cast or with a parenthesized expression list

And from List initialization:

Direct-list-initialization

T object { arg1, arg2, ... }; (1)

T { arg1, arg2, ... } (2)


Now we can use the above to answer your question.

S s1{1};    // direct-list-initialization from direct-list init point 1 above
S s2{1, 2}; // direct-list-initialization from direct-list init point 1 above
S s3(1);    // direct-initialization from direct init point 1 above
S s4(1, 2); // direct-initialization from direct init point 1 above
int i1{10}; // direct-initialization from direct init point 2 above
int i2(10); // direct-initialization from direct init point 1 above