optional icon indicating copy to clipboard operation
optional copied to clipboard

optional<T&> should sometimes be convertible-to optional<U&>

Open Quuxplusone opened this issue 1 year ago • 0 comments
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).

Quuxplusone avatar Sep 08 '24 19:09 Quuxplusone