Skip to content
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

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

Open
Quuxplusone opened this issue Sep 8, 2024 · 0 comments
Open

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

Quuxplusone opened this issue Sep 8, 2024 · 0 comments

Comments

@Quuxplusone
Copy link

Quuxplusone commented Sep 8, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant