According to the standard, the copy-constructor of std::optional<T>
:
...shall be defined as deleted unless
is_copy_constructible_v<T>
istrue
.
But the move-constructor of std::optional<T>
:
...shall not participate in overload resolution unless
is_move_constructible_v<T>
istrue
.
As I understand deleted constructors, the purpose of not-deleting the move-constructor of std::optional<T>
would be to allow code like this:
std::optional<X> o1;
std::optional<X> o2(std::move(o1));
...to work relying on some conversion sequence - o2
would be constructed by an object of type A
that has been constructed using a std::optional<X>&&
(correct me if I am wrong).
But regarding the possible constructors of std::optional
, I have a hard time figuring out one that could match this use case...
Why is the move-constructor of std::optional<T>
simply not deleted if T
is not move-constructible?
To explicitly delete it means that it will be the best match for x-values, and thus result in a compile-time error, rather than the copy-constructor taking those cases.
Ex:
This will result in something like:
Explicitly deleted function still participate in overload resolution, and can be the best match. This can be useful, to disable certain conversions for example.