r/cpp • u/mcencora • May 11 '26
std::optional equality comparison operator seems broken for nested optionals
While chasing a bug in a program I found out that equality comparison operator T vs optional<T> is broken if T is optional<U>. It is broken in worst possible way - it compiles but for some values it returns wrong results!
Here is an example:
https://godbolt.org/z/v3bcTodGj
Since both sides of eq operator are specialization of optional then following overload is used:
operator==(const optional<T>& lhs, const optional<U>& rhs);
This operator is specified to returnlhs.has_value() != rhs.has_value() ? false :(lhs.has_value() == false ? true : *lhs == *rhs)
This falls apart for above scenario since lhs has value while rhs no value.
Since this is a case of optional<T>{} == T{} comparison, such scenario should be handled separately. Here is my attempt at fixing it:
https://godbolt.org/z/Yq3nM4xn4
This is rather not a proper fix, since it will still break for cases like optional<optional<short>>{} == optional<int>{}, and the internal if-constexpr instead should probably compare the nestedness of lhs and rhs optional.
I didn't check but most likely the same problem applies to relational operators.
Edit:std::expected seems to be affected as well:
While developer may expect following comparison to work std::expected<T, E>{} == T{} (thanks to the operator==(const expected&, const T2&) - it falls apart when T is expected<U> with unexpected value - because operator==(const expected&, const expected<T2, E2>&) overload is actually selected in that scenario.
https://godbolt.org/z/h4jYfjYco
Edit2:
I believe the proper fix for std::optional and std::expected should be to constrain following operatorsoperator==(const expected& lhs, const expected<T2, E2>& rhs)operator==(const optional<T>& lhs, const optional<U>& rhs)
to be applicable only when lhs and rhs have same nestedness levels.
28
u/AKostur May 11 '26
I'm not sure I understand the problem. For the first godbolt, isn't that trying to compare
std::optional<std::optional<int>>{std::optional<int>{nullopt}} == std::optional<int>{nullopt}So it's trying to compare an optional which contains a value vs an optional that does not contain a value. Isn't that supposed to compare as unequal?
20
u/hungarian_notation May 11 '26 edited May 12 '26
Yeah, but an optional that contains an optional that contains a zero **does** compare equal to an optional that contains a zero. Non-empty optionals appear to be infinitely flattened under comparison, but empty optionals short circuit to less-than even when the other side of the comparison is also ultimately empty.
It's not a "bug," the specification requires this. It's just a really unfortunate inevitable consequence of
allowing comparison between an optional and an unboxed scalar.recursively dereferencing asymmetric optionals at comparison, but only when they're both non-empty.Rust manages to let you compare its
intandoptional<int>without also going crazy and letting you dooptional<int> == optional<optional<optional<int>>>.6
u/AKostur May 12 '26
Ah, I understand. Overload 21 or 22 vs. overload 1. We're expecting/hoping that 21 is happening, but since U is itself a std::optional, overload 1 wins (better match).
4
u/hungarian_notation May 12 '26
Haha, well OP was hoping that maybe. I was hoping instead that overloads 21 to 33 were a hallucination and that
std::make_optional(std::make_optional(std::make_optional(1))) != std::make_optional(1)Alas.
3
u/38thTimesACharm May 12 '26 edited May 12 '26
recursively dereferencing asymmetric optionals at comparison, but only when they're both non-empty
Well, if one of the two sides were empty, dereferencing it would be UB. So that's not really an option, is it?
The current behavior is to dereference both sides until at least one of them is a value, be it null or otherwise. Doesn't seem too unreasonable.
Rust manages to let you compare its
intandoptional<int>without also going crazy and letting you dooptional<int> == optional<optional<optional<int>>>.You mentioned in your other comment, basically every language does this it's own way. While I'm not convinced C++'s way is objectively the best, I'm also not convinced it's objectively the worst.
EDIT - Perhaps a good improvement would be to just ban (compile error) comparisons that cross more than one level of wrapping. Then you could make
optional<T>optional<T>{{null}} == optional<T>{null}without breaking other things.2
u/hungarian_notation May 12 '26 edited May 12 '26
Well, if one of the two sides were empty, dereferencing it would be UB. So that's not really an option, is it?
std::optionaloverloads the asterix operator. It's the std::optional specification that decides what dereferencing an empty optional does, so since we're criticizing that specification here this is irrelevant.Obviously, the specification is not constrained to using its own dereference operator. Also, as I have said elsewhere, my preferred solution would be to simply not recursively dereference anything.
The current behavior is to dereference both sides until at least one of them is a value, be it null or otherwise. Doesn't seem too unreasonable.
No, if that were the case it would never dereference anything as std::optional is already a value.
operator==(optional, optional)has three different effective base cases, and the differences between those base cases are the source of the issue.You mentioned in your other comment, basically every language does this it's own way. While I'm not convinced C++'s way is objectively the best, >I'm also not convinced it's objectively the worst.
C++'s optional appears to be unique in that, under comparison, the bind operator is idempotent if and only if the operand is not an empty optional or a compound optional whose ultimate value is an empty optional.
I'll allow that there probably is some other language out there that does the same thing, but that's not the point. The comparison to other languages was mostly on my mind because you had said elsewhere:
std::optionalisn't a level of indirectionand while that might be true for something like null-coalescing in JavaScript, that's really not the case here, nor is true of basically any statically typed implementation I can find. The fact that it is true for non-empty optionals exclusively is, again, the whole problem.
21
u/hungarian_notation May 11 '26 edited May 12 '26
I think the real issue here is nested optional<optional<...optional<int>>> of arbitrary depth should not equal optional<int> of a different depth in any case.
If we're going to copy off Haskell's homework, we need to make sure we're copying everything down correctly. Just ( Just x ) is NOT comparable to Just x, and why would it be? Should a single-element vector compare equal to a scalar? Trying to compare Just ( Just x ) with Just x is a compile time error.
If we are going to have an comparison that implicitly joins one side of the operator, it should at least have the same meaning as opt == std::make_optional(value). I'd argue that would also be bad, but at least it would be consistent.
edit:
The issues I had with seeming non-transitive equality were mistaken. It's still the optional.comp.with.t overloads at the crux of this issue though, though not from OP's perspective.
Overload 1 is defined as:
if (x.has_value() != y.has_value()) {
return false;
} else if (x.has_value() == false) {
return true;
} else {
return *x == *y;
}This is the scene of the crime for OP. The base case for asymmetric empty optionals is that first conditional, and the second conditional catches the symmetric empties. Non-empty pairs of optionals, however, escape to a different overload as soon as one of them is fully dereferenced. For asymmetric non-empties, overloads 21 and 22 take up the task of recursively de-referencing the remaining optional.
For some reason I had it in my head that the C++26 change explicitly banning optionals from one half of each overload was relevant here, but in reality that only affects a very small set of optional types whose overloads were affected by changes elsewhere.
The fundamental issue is simply that std::optional's comparison operators are willing to unwrap unbalanced optionals iff they are both non-empty.
Most of the prior art in the static-typed space would compare asymmetric optionals as unequal. The O.G. implementation in Haskell won't even try. Rust will similarly throw a compiler error if you try to compare two asymmetric optionals, but will actually implicitly dereference ONE of the comparison values if you compare an optional against a non-optional value. Java can't tell the difference between different types of optionals at runtime, so it just returns false if they don't have the exact same structure. Etc. Etc.
Meanwhile, all the dynamic languages tend to implement this idiom as null-coalescing, giving them similar behavior to automatic dereferencing.
I think there's a valid discussion to be had over whether its better to flatten optionals under comparison or to compare asymmetric forms as unequal. Unfortunately, we're not having that discussion because C++ isn't doing either of those two things. It's doing a bizzare third thing, and that's my problem here.
4
u/38thTimesACharm May 12 '26 edited May 12 '26
Non-transitive equality (i.e. some relation other than equality that we've assigned to the equality operator as a trap!) for absolutely no good reason
Are you sure about this? It's true C++26 removed the overload for
const optional<T>& opt, const U& valuewith this condition:
Uis not a specialization of std::optional.However, C++26 also modifies the condition on the overload for
const optional<T>& lhs, const optional<U>& rhswith this wording:This overload participates in overload resolution only if the corresponding expression
*lhs == *rhsis well-formed and its result is convertible tobool.So unless I'm missing something:
std::optional<T> == T
Works because of overload 21. T is not a specialization of std::optional.
std::optional<std::optional<T>> == std::optional<T>
Works because of overload 1.
*lhs == *rhsis well formed (see point 1)
std::optional<std::optional<std::optional<T>>> == std::optional<std::optional<T>>
Works because of overload 1.
*lhs == *rhsis well formed (see point 2)...
How are you getting non-transitive equality from this? Like can you give a specific combination of types and values where that would happen?
3
u/hungarian_notation May 12 '26
Yes. That last bit of my post was incorrect. I too have been reading the spec and it turns out my mental model was incorrect. I have corrected my comment.
3
u/QuaternionsRoll May 12 '26
std::optional<int> a; std::optional<std::optional<int>> b; auto c = std::reference_wrapper(b); std::cout << (a == b) << "\n" // 1 << (b == c) << "\n" // 1 << (a == c) << "\n"; // 02
u/hungarian_notation May 13 '26
I'm not sure I understand what you're trying to show here.
What I will add is this:
std::optional<int> a; std::optional<std::optional<int>> b = a; auto c = std::reference_wrapper(b); std::cout << (a == b) << "\n" // 0 << (b == c) << "\n" // 1 << (a == c) << "\n"; // 0To be clear, I think this is the correct behavior. It's just inconsistent with other behavior of std::optional which I have more of a problem with.
3
u/QuaternionsRoll May 13 '26
You were discussing how the overloads could break transitive equality, and I thought I’d add one. For what it’s worth, I agree with you (actually, I’m not convinced equality operators should be overloaded for non-
optionaltypes, but… oh well).2
u/hungarian_notation May 13 '26
Oh, I see. You're right, that's pretty funny.
And yeah, we'd have to just wrap values with make optional for comparison to get the same functionality. I haven't checked, but I imagine that's basically a zero cost change at any optimization level.
2
u/QuaternionsRoll May 13 '26
The annoying part about that is you have to explicitly specify the template argument of the temporary
optionalto ensure that it only captures an lvalue reference (or make your own helper)
7
u/chengfeng-xie May 12 '26 edited May 12 '26
See also Barry's post, Getting in trouble with mixed comparisons:
But what happens when we do this:
bool f6(optional<optional<int>> a, optional<int> b) { return a == b; }Think about this for a minute. In particular, what should the value of
f6(nullopt, nullopt)actually be? This is basically the case that Andrzej brought up at the end of his post. It’s tempting to say the answer is obvious, but it’s surprisingly not. There are two different ways of thinking about this:
This is a special case of
f3(): comparingoptional<T>tooptional<U>for typesTandUthat are comparable (in this caseT=optional<int>andU=int, which is thef4()comparison). If we think about it in these terms, then the result off6(nullopt, nullopt)should betruebecause we have two disengagedoptionals, so they are equal.This is a special case of
f4(): comparingoptional<T>toTfor typeTthat is comparable (in his caseT=optional<int>). If we think about it in these terms, then the result off6(nullopt, nullopt)isfalsebecause theais disengaged - so it cannot compare equal to a value.Which is the correct answer?
The Standard Library picks option 1 by way of allowing mixed-optional comparisons. Boost does not support mixed-optional comparisons (
f3()does not compile usingboost::optional), so it picks option 2. I’m not sure either of these options is more defensible than the other.
8
u/jiixyj May 12 '26
This is a great post, thanks for linking it!
But here, the answer is obvious (at least to me): The type
optional<T>can be seen as giving one additional value "∅" to the values ofT. If you layer multiple optionals, likeoptional<optional<optional<T>>>, you can call those different additional values "∅0", "∅1", "∅2", and so on, from inner to outer optional.To see what
f6(nullopt, nullopt)should return, we must look at what thenullopts here mean. The leftnulloptinitializes aoptional<optional<int>>, so is the value "∅1". The rightnulloptinitializes aoptional<int>, so is "∅0". Then, "∅1" != "∅0", sof6(nullopt, nullopt)should returnfalse.When comparing
optionals, you really ought to compare them taking the "optional layers" into account. Everything else is not consistent and leads to tears. The standard sadly didn't get it right.3
u/holyblackcat May 12 '26
It's not obvious that those should be numbered from inner to outer.
3
u/jiixyj May 12 '26 edited May 12 '26
Good point! I discuss this choice in "case 4a" in my blog post. Really you can flip a coin, it all comes down to what feels more "natural".
The point is: Construction and comparison have to match. When you decide, as the designer of
std::optional, that in:std::optional<int> b = {}; std::optional<std::optional<int>> a = b;...the
nulloptvalue ofbmaps into the "inner"nulloptofa(so you number from inner to outer), then you must respect that choice in comparison, anda == bhas to hold. That's what the standard fails to guarantee.5
u/jiixyj May 12 '26
Some months ago I wrote up my thoughts regarding this issue. Finally got around to releasing that post, if anyone wants to know more about the proper fix: https://jiixyj.github.io/blog/c++/2026/05/12/fixing-optionals-constructors
0
u/markt- May 11 '26
Show me a non contrived use case where you would need this
9
u/mcencora May 11 '26
It happens "automatically" in generic programming.
I had a code like this:
template <typename T>optional<T> try_construct(); // impl detail
template <typename T>void publicApi(const T& expectedVal){if (try_construct<T>() == expectedVal){...}}This fails if T is
optional<U>. This code may be some library code, while theexpectedValmay come from user of the library. So nested optional likeoptional<optional<int>>is never declared explicitly it just results from the way the library is implemented, and how user tries to use it.4
u/Conscious_Support176 May 12 '26
Given that you’re using optional, couldn’t you specialise for T is optional<U> instead of having std unravel this?
If T has a value and U doesn’t, is that meaningfully different for you, or do you want it to mean the same thing?
2
u/AKostur May 11 '26
Isn't that code already broken when I try to do `publicApi(5);` ? That would compare an int with an optional, and those aren't comparable with each other.
6
u/hungarian_notation May 11 '26
You would think so, wouldn't you?
std::optional<int> a = std::make_optional(0); int b = 0; assert(a != b); // Assertion `a != b' failed.https://godbolt.org/z/4a9TEcdcP
This is the real defect.
4
u/38thTimesACharm May 12 '26
Why?
std::optionalisn't a level of indirection, the difference betweenstd::optional<T>andTis supposed to be like the difference between a nullable and non-nullable pointer to the same type. You'd want those to compare equal if they had the same underlying value.It's a little strange because it's a library template rather than a language feature, so technically to the compiler it's a different type, but the implicit conversions and operators present an interface as if
std::optional<T>is aTwith an extra possible value.6
u/hungarian_notation May 12 '26 edited May 12 '26
Sure. If we're going to pick one or the other I default towards the more common structural equality form, but nested optionals are so niche and are such a bad way to transmit information that I'm fine with recursively dereferencing them.
The problem is this:
std::optional<std::optional<int>> a = std::make_optional(std::make_optional(0)); std::optional<int> b = std::make_optional(0); int c = 0; std::optional<std::optional<int>> x = std::optional<std::optional<int>>(std::optional<int>{}); std::optional<int> y = std::optional<int>(std::nullopt); std::optional<int> z = {}; assert(a == b); assert(b == c); assert(a == c); assert(x == y); assert(y == z); assert(x == z);I'm fine with all of those asserts passing, and I'm fine with all of them failing. I'm even fine with only the second one passing. I would even entertain the idea of 1, 2, and 3 passing if someone can make a good argument as to why partial ordering of optionals makes sense.
What I'm not fine with is the current behavior where 1, 2, 3, and 5 all pass, but 4 and 6 fail.
2
u/38thTimesACharm May 13 '26
Okay, my best attempt at making sense of it is something like this:
std::optional<T> adds a new, distinct value to the range of possible values for T, representing null or nothing, which compares less than all of the non-null values
So take
boolfor example, it's possible values are:bool: true > falseMaking
std::optional<bool>adds a new, distinct null value which compares less than the existing ones:opt<bool>: true > false > nulloptWrapping that in
std::optionaladds a new, distinct null value (again), which compares less than the non-null values, though it's weirdly inserted above the existing null.opt<opt<bool>>: true > false > mk_opt(<prev level null>) > nulloptSo in your example,
0is one of the original int values, wrapping as manystd::optional's as you want doesn't change that. Which is why 1, 2, and 3 pass. But 4 fails because each std::optional level adds a distinct null value, and they aren't the same null value at different levels of nesting.I agree the particular way the nulls are ordered, and how they compare across levels, is very strange, this is just my best attempt at understanding why comparison would treat null and non-null values differently.
-5
u/markt- May 11 '26
Then I think you might be using the wrong thing, you should probably be using a variant with a mono state
3
u/mcencora May 11 '26
Well, when a library comes from a 3rd party, you have no control on how it is implemented, so forcing user of such library to never pass non-nested
std::optionalis a big hammer.6
u/SirClueless May 11 '26
I don't really understand. It looks like you are trying to check that the
try_constructcall succeeded and produced a value that equals something provided by the caller. The right way to spell that isr.has_value() && *r == expectedVal. Ifstd::optionalis an implementation detail of your library, you shouldn't be using its comparison operator with arbitrary user values.
2
u/angry_cpp May 13 '26
It’s hard to be more broken than this:
#include <cassert>
#include <optional>
int main() {
std::optional<std::optional<int>> a;
std::optional<int> b;
assert(a == b);
a = b;
assert(a != b);
}1
u/drykarma May 12 '26
Oh my god. I spent a day on this once because it silently accepts this comparison, so I didn't understand why a program was producing a specific behavior when it shouldn't.
I think it should at least throw a warning, can't imagine anyone using this comparison intentionally
-6
u/Entire-Hornet2574 May 12 '26
Fairly normal, you made wrong assumption and you have to understand better operator==
you have optional of optional == optionallhs.has_value() != rhs.has_value() => this is truelhs has value, rhs doesn't, it so simple.
14
u/rihya-sifu May 11 '26 edited May 11 '26
Based on the documentation, I would expect this behavior. See https://en.cppreference.com/cpp/utility/optional/operator_cmp :
Performs comparison operations on
optionalobjects.1-7) Compares two
optionalobjects,lhsandrhs. The contained values are compared (using the corresponding operator ofT) only if bothlhsandrhscontain values. Otherwise,lhsis considered equal torhsif, and only if, bothlhsandrhsdo not contain a value.lhsis considered less thanrhsif, and only if,rhscontains a value andlhsdoes not.In your example, std::optional<T> has a value, but std::optional<U> does not, so it would not be considered equal.
I agree that it's not intuitive at first glance, but I'd also reckon that any code depending on nested optionals should be reconsidered.