-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why is the ctor of optional<T&> explicit? #99
Comments
This is for consistency with the non-reference specialization: The missing boost::optional<int> oi;
boost::optional<const int> oci = oi; We could consider special casing the |
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
So it might make sense to make |
Unless the binding of temporaries to lvalue references to int i = -1;
unsigned const& ui = i; Consistency with template <typename T, typename U>
void test_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>();
|
Yes, I meant converting lvalue references of course (there is no boost::optional<T&&> anyway).
I see. |
Would allowing a conversion from |
Well, the absence of the |
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.
The text was updated successfully, but these errors were encountered: