Given the following code:
#include
#include
struct foo
{
explicit operator std::optional() {
return std
Since this is direct-initialization, we enumerate the constructors and just pick the best one. The relevant constructors for std::optional are :
constexpr optional( const optional& other ); // (2)
constexpr optional( optional&& other ) noexcept(/* see below */); // (3)
template < class U = value_type >
/* EXPLICIT */ constexpr optional( U&& value ); // (8), with U = foo&
Both are viable ((8)
only participates in overload resolution if int
is constructible from foo&
and foo
is neither std::in_place_t
nor std::optional
, all of which hold), but (8)
is an exact match whereas (2)
and (3)
require a user-defined conversion, so it should be preferred. gcc is wrong here.
However, gcc doesn't actually invoke (3)
either. It just directly initializes my_opt
from the result of converting my_foo
to an optional
. This program with gcc 7.2 prints 3
but none of 1a
, 1b
, or 2
:
#include
template
struct opt {
opt() { }
opt(opt const& ) { std::cout << "1a\n"; }
opt(opt&& ) { std::cout << "1b\n"; }
template
opt(U&& ) { std::cout << "2\n"; }
};
struct foo
{
explicit operator opt() { std::cout << "3\n"; return {}; }
};
int main()
{
opt o(foo{});
}
I don't think that's an allowable route. I filed 81952.