optional
optional copied to clipboard
optional<T&> should sometimes be convertible-to optional<U&>
trafficstars
TEST_CASE("issue 66") {
int i = 42;
tl::optional<int&> a = i;
tl::optional<const int&> b = a;
REQUIRE(&b.value() == &i);
b = a;
REQUIRE(&b.value() == &i);
}
I'd like this to compile, but it doesn't.
tl-optional/tests/issues.cpp:50:30: error: no viable conversion from 'optional<int &>' to 'optional<const int &>'
tl::optional<const int&> b = a;
^ ~
However, I understand that making this work is difficult pre-C++20 because of the lack of "explicit(bool)," so this might be deliberately "out of scope" for tl::optional. If so, feel free to close as WONTFIX.
Oddly, the assignment operator b = a; does compile and seems to work correctly, even though the converting constructor is missing. Even if you don't add the converting ctor (because difficult), you might consider removing the converting assignment operator (for self-consistency).