I'm trying to define an equality operator for a type T defined in another namespace, and then use the equality operator on optional<T>
. On clang (Apple LLVM 9.1.0), this code:
namespace nsp {
struct Foo {
};
}
bool operator==(const nsp::Foo& a, const nsp::Foo& b);
void foo() {
optional<nsp::Foo> a = none;
optional<nsp::Foo> b = none;
if (a == b)
;
}
Results in an error:
/usr/local/include/boost/optional/detail/optional_relops.hpp:29:34: error: invalid operands to binary expression ('const nsp::Foo' and 'const nsp::Foo')
{ return bool(x) && bool(y) ? *x == *y : bool(x) == bool(y); }
~~ ^ ~~
MWE.cpp:40:19: note: in instantiation of function template specialization 'boost::operator==<what3words::engine::nsp::Foo>' requested here
if (a == b)
^
/usr/local/include/boost/optional/detail/optional_relops.hpp:28:6: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'const nsp::Foo'
bool operator == ( optional<T> const& x, optional<T> const& y )
What's happening? My guess is that it's something to do with the Koenig lookup rules...
Boost C++ Libraries Class template optional is a wrapper for representing 'optional' (or 'nullable') objects who may not (yet) contain a valid value. Optional objects offer full value semantics; they are good for passing by value and usage inside STL containers.
boost::make_optional() can be called to create an object of type boost::optional . If you want a default value to be returned when boost::optional is empty, you can call boost::get_optional_value_or() .
The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions.
The equality operators in C++ are is equal to(==) and is not equal to(!=). They do the task as they are named. The binary equality operators compare their operands for strict equality or inequality.
Do this:
namespace nsp {
bool operator==(const Foo& a, const Foo& b);
}
to fix your problem.
If you have control over Foo
, you can instead do:
namespace nsp {
struct Foo {
friend bool operator==(const Foo& a, const Foo& b) {
return true;
}
};
}
which is optimal if Foo
is a template class.
What is going on here is that optional
is in std
(or boost
or whatever) and in that namespace it tries to do a nsp::Foo == nsp::Foo
call.
There is a ==
that doesn't apply in the ::std
namespace, so it won't look in ::
; once it finds any ==
it stops looking, even if the arguments are completely unrelated. It also looks for ==
in the namespaces associated with the arguments -- in this case ::nsp
. But it never looks in ::
here either.
When adding operators to a type, always define the operator in the namespace of the type.
Namespaces can be reopened. So if you don't have control over the header file, you can create a new header file with the ==
in it. This ==
has to be visible at every point where optional<nsp::Foo>::operator==
is invoked or your program is ill formed due to ODR violations (and, in this case, also generates a compiler error, which is useful to avoid heizenbugs).
When you invoke an operator (or a function), lookup follows a few simple steps.
First it looks around locally (in the local namespace). If it finds anything there, this search stops. (This includes using ns::identifier;
names injected into the namespace, but usually not using namespace foo;
). This "stop" occurs even if the function or operator won't work for the types in question; any ==
for any types whatsoever in a namespace stops this search.
If that fails to find a match, it starts looking in enclosing namespaces until it finds the function/operator, or reaches the root namespace. If there are using namespace foo;
declarations, the functions/operators in those namespaces are considered to be in the "common parent" namespace of both the using namespace
location and the namespace being imported. (So using namespace std;
in namespace foo
makes it seem like std
is in ::
, not in foo
).
The result generates a collection of candidates for overload resolution.
Next, ADL (argument dependent lookup) is done. The associated namespaces of all function/operator arguments are examined. In addition, the associated namespaces of all the type arguments of the templates are also examined (recursively).
Operators/functions that match the name are collected. For ADL, parent namespaces are not examined.
These two collections of operators/functions are your candidates for overload resolution.
In your case, the namespace where ==
is called is boost
. boost
has plenty of ==
operators (even if they don't apply), so all of the ==
in boost
are candidates.
Next, we examine the namespace of the arguments -- nsp::Foo
in this case. We look in nsp
and see no ==
.
Then we run overload resolution on those. No candidates work, and you get a compiler error.
Now, when you move your user-defined ==
into namespace nsp
, it is then added to the set of ==
found in the ADL step. And as it matches, it is called.
Overload resolution (what it does with the candidates) is its own complex subject. The short form is that it tries to find the overload that involves the least amount of conversion; and if two cases match exactly as well as each other, it prefers non-template over template and non-variardic over variardic.
There is a lot of detail in "least amount of conversion" and "exactly" that can mislead programmers. The most common is is that converting a Foo
lvalue to Foo const&
is a small amount of conversion, convering it to template<class T> T&&
or T&
is no conversion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With