You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I mean the ctor template <class U> explicit optional(const optional<U&>& rhs). It doesn't seem to have much sense - optional<T&> is logically a reference and it seems reasonable to allow it to be converted implicitly if the underlying pointer types allow this.
With the current implementation the usage of optional references can be cumbersome, because even the non-const-ref to const-ref conversion has to be explicit.
I see. But does this consistency actually make sense? I mean, for value types the implicit conversion can indeed do nasty stuff like slicing or narrowing, but converting references seems to always be benign.
Btw, both kinds of optional have assignment operators corresponding to explicit ctors, which kind of defeats the purpose, because you can still perform an implicit conversion:
boost::optional<float> of;
boost::optional<int> oi;
oi = of;
So it might make sense to make boost::optional<T> behave like std::optional in this regard, i.e. the converting ctor is implicit if the underlying types are implicitly convertible to each other (and the assignment is present only of the underlying types are assignable). In this case making ctors of boost::optional<T&> implicit will actually make it consistent with boost::optional<T>.
Unless the binding of temporaries to lvalue references to const is explicitly disabled., even the reference casts have the same problems:
int i = -1;
unsignedconst& ui = i;
Consistency with std::optional is not necessarily a good thing, as std::optional has design flaws that cause ambiguities:
template <typename T, typename U>
voidtest_optional()
{
std::optional<U> ou = std::nullopt;
std::optional<T> ot = ou;
assert (ot == std::nullopt); // true for some Ts and Us, false for other Ts and Us.
}
The above looks like an obvious thing to expect, but fails for
test_optional<std::optional<int>, int>();
boost::optional has fewer gotchas like the one above, because it does not try to be 100% consistent with std::optional.
Well, the absence of the T& to const T& conversion is what caused me to report the issue.
But IMO the conversion from Derived& to Base& would also be nice to have.
Activity
akrzemi1 commentedon Nov 24, 2021
This is for consistency with the non-reference specialization:
optional<U>is not implicitly convertible tooptional<T>even ifUis convertible toT.The missing
T-to-const Tcan indeed be disturbing. This is also the case for non-references:We could consider special casing the
T-to-const Tconversion.ImplOfAnImpl commentedon Nov 24, 2021
I see. But does this consistency actually make sense? I mean, for value types the implicit conversion can indeed do nasty stuff like slicing or narrowing, but converting references seems to always be benign.
Btw, both kinds of
optionalhave assignment operators corresponding to explicit ctors, which kind of defeats the purpose, because you can still perform an implicit conversion:So it might make sense to make
boost::optional<T>behave likestd::optionalin this regard, i.e. the converting ctor is implicit if the underlying types are implicitly convertible to each other (and the assignment is present only of the underlying types are assignable). In this case making ctors ofboost::optional<T&>implicit will actually make it consistent withboost::optional<T>.akrzemi1 commentedon Nov 24, 2021
Unless the binding of temporaries to lvalue references to
constis explicitly disabled., even the reference casts have the same problems:Consistency with
std::optionalis not necessarily a good thing, asstd::optionalhas design flaws that cause ambiguities:The above looks like an obvious thing to expect, but fails for
boost::optionalhas fewer gotchas like the one above, because it does not try to be 100% consistent withstd::optional.ImplOfAnImpl commentedon Nov 24, 2021
Yes, I meant converting lvalue references of course (there is no boost::optional<T&&> anyway).
I see.
akrzemi1 commentedon Nov 24, 2021
Would allowing a conversion from
Ttoconst Taddress your issue?Or conversion from
Derived&toBase&?ImplOfAnImpl commentedon Nov 24, 2021
Well, the absence of the
T&toconst T&conversion is what caused me to report the issue.But IMO the conversion from
Derived&toBase&would also be nice to have.