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

Comparison of std::optional<not_null<T>> and not_null<T> is ambiguous #346

Open
MatthewNielsen27 opened this issue Oct 27, 2023 · 2 comments

Comments

@MatthewNielsen27
Copy link

The comparison of std::optional<not_null<T>> and not_null<T> is ambiguous. This is possibly because they both have overloads of the equality operator on their "nullable" types, which are of equal precedence.

Here's a minimal example to repro: https://godbolt.org/z/xnEfP1acb

It should be noted that Microsoft/GSL doesn't have this issue: https://godbolt.org/z/Ed7qG5EE8

@mbeutel
Copy link
Collaborator

mbeutel commented Oct 28, 2023

Thanks for the report.

I have yet to come up with a convincing approach to solve this problem; suggestions are welcome. We might be tempted to follow MS-GSL and only define comparison operators for two not_null<> arguments (link) rather than the set of mixed-argument overloads we have now (link); but the simpler approach is fraught with other problems: for example, comparing std::shared_ptr<T> and not_null<std::shared_ptr<T>> then fails, whereas it works fine with gsl-lite. (Repro for MS-GSL, gsl-lite.)

As an aside, isn't optional<not_null<T>> just a contrived semantic equivalent of T?

@MatthewNielsen27
Copy link
Author

As an aside, isn't optional<not_null> just a contrived semantic equivalent of T?

Yeah, kinda. But when designing generic types / interfaces with value semantics, this issue adds some mental overhead. The pattern is used frequently in Rust, with Option<Box<T>> which is equivalently optional<not_null<T*>>.

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

2 participants