Copy constructor of optional using term "direct-non-list-initializes" instead of "copy constructs"

58 views Asked by At

In C++ standard the copy constructor of optional is defined as follows:

constexpr optional(const optional& rhs);

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *rhs.

If rhs contains a value, it will get copy constructed and since the copy constructor is defined as deleted unless is_copy_constructible_v is true, why does the standard use the more generic "direct-non-list-initialization" instead of saying "If rhs contains a value, it 'copy constructs' the contained value with *rhs"?

1

There are 1 answers

2
273K On

If A a, then both A a1(a) and A a2{a} are direct-initialized. However, if A has an initializer-list constructor, it will always be preferred over other constructors for brace-init-initializers (list initialization). If A has both a copy constructor and an initializer-list constructor, the latter is used for A a2{a}.

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *rhs.

The standard requires that the direct-initializing copy constructor A(const A&) is used, and the direct-list-initializing constructor must not be used if a copy constructor is deleted.