Why does implicit == on map<<int,MyClass> not compile? - c++

I have a strange issue defining == for one of my class.
I simplified the code here to an example that i tested on my visual 2013;
MyClass is defined in namespace N
This does compile:
N::MyClass a, b;
bool test = a == b;
This too:
const N::MyClass a, b;
bool test = a == b;
This does not compile
std::map<int, N::MyClass> a, b;
bool test = a == b;
For your information the == operator is declared like this :
bool operator==(const N::MyClass & a, const N::MyClass & b);
here is the error I get : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const MyClass' (or there is no acceptable conversion)
But from what I know the map operator is defined : map == reference on cppreference.com
could someone explain me why this is wrong ?
Thank you in advance.
I did not find an answer and I'm sorry if this is stupid.
[solution]
If I mode the operator into the namespace it works :
bool N::operator==(const MyClass & a, const MyClass & b);
But I don't know why I need to do that, is it in the language definition ? (I guess yes)

Based on your description, I guess your equality operator is not defined in the same namespace as your class. The following demonstrates the situation:
#include <map>
namespace foo
{
class bar
{
};
}
using namespace foo;
bool operator== (bar const&, bar const&) { return true; }
int main()
{
bar const b;
b == b; // OK
std::map<int, bar> mb;
mb == mb; // ERROR
}
The obvious fix is to define the equality operator in the same namespace as the class. The reason it doesn't work is the two-phase name look-up in templates: when a templates is instantiated it only does second phase name look-up, i.e., it only finds functions relating to template arguments based on explicit qualification or argument dependent look-up. The equality operator isn't qualified in std::map<K, V> (well, it is never qualified when using operator notation to call it) and, thus, it needs to be found by argument dependent look-up.

Your MyClass::operator== needs to be marked const. For example:
bool operator ==(const MyClass&) const;
Note that your operator:
bool operator==(const MyClass & a, const MyClass & b);
will not work. operator== only takes one argument and compares this and the passed argument.
Unless it's not a class member operator, but a non-member one. In this case, it's correct and should work. Since it doesn't, it suggests to me that you forgot to #include the header file where your operator is declared.

Related

Implicit commutativity of operator overloading

In C++, operators can be overloaded. If I have two classes A and B and for instance I can overload operator == in class A for comparison with an instance of B:
class A {
// Some attributes, methods
public:
bool operator ==(const B &b) {
// Implementation of operator ==
}
}
then I can run code like:
A a;
B b;
if(a == b) {
// Some code
}
However, if I write b == a, the compiler will look for
bool operator ==(const A &a) in class B. If it is not found, is there a default behavior in C++ normalization? Will the compiler, whatever it is, exit on failure or run bool operator ==(const B &b) instead?
And even better, if it does not find operator < () in class B, will it attempt operator >=() in class A?
No, it won't do either. There's nothing special about overloaded operators; they're just overloaded functions with funny-looking syntax. So a == b in the code above is nothing more than a call to a.operator==(const B&).
The reason that b == a fails, of course, is that there is no b.operator==(const A&) (and, similarly, no global operator).
The usual way to deal with this issue is to have a global operator== rather than a member: bool operator==(const A&, const B&), and another global operator== that goes the other way: bool operator==(const B&, const A&); if the comparison is, in fact, symmetric, then the second one can be trivially implemented by calling the first: bool operator==(const B& b, const A& a) { return a == b; }.
The same principle applies to operator<: it doesn't go the other way, and if you want one that does, you have to write it.
(small print: please ignore -- there is std::relops (spell-correction wants to change "relops" to "relapse") that will provide these variants; don't use it; it's far too invaisve.)

Unable to find operator defined in the global namespace

Consider the following example:
namespace X {
struct A { static const A aaa; };
const A A::aaa = {};
}
bool operator == (const X::A &a, const X::A &b) { return true; }
bool workaround (const X::A &a, const X::A &b) { return a == b; }
namespace Y {
using X::A;
struct B { const A & an_a() const { static A a; return a; } };
bool operator == (const B &a, const B &b) { return true; }
bool foo (const B &b) {
return b.an_a() == A::aaa;
//return workaround(b.an_a(), A::aaa);
}
}
int main () {}
This code fails to compile foo because the == operator resolves the one in namespace Y rather than the one in the global namespace.
.code.tio.cpp:17:25: error: invalid operands to binary expression ('const X::A' and 'const X::A')
return b.an_a() == A::aaa;
~~~~~~~~ ^ ~~~~~~
.code.tio.cpp:14:10: note: candidate function not viable: no known conversion from 'const X::A' to 'const Y::B' for 1st argument
bool operator == (const B &a, const B &b) { return true; }
^
1 error generated.
I think I understand this is due to the way ADL has been defined for C++, which seems to explicitly exclude searching the global namespace if the class belongs to a namespace. But, it still seems counter-intuitive to me. Is there an explanation of why the global namespace could not be included in the set of namespaces to be searched for operator==?
For consistency, operators are considered to be just function names, and a function hides any in an enclosing scope (even if the parameter types differ). (The latter rule is presumably intended to prevent unexpected overload resolution interaction between functions declared in different scopes).
Your ::operator==(const X::A&,const X::A&) is hidden (within Y) by Y::operator==(const B&,const B&), and it's not considered to be part of the interface of X::A (because it's not in the immediate namespace X), so you can't find it.
If you're willing to change the spelling of the comparison, you can use the ugly, explicit ::operator==(b.an_a(), A::aaa) instead of adding workaround.

Non-friend single-line operator + overload based on operator +=

Let's say I have a class:
class A
{
//...
};
With well defined operator +=:
A& operator +=(const A&) {return *this;} //class without members
So let's try to overload operator+ also (as non-friend). I don't want to use class name to call constructor of temporary object (kinda want this make generic code):
A operator +(const A& other) const
{
return auto(*this)(*this) += other; //error: invalid use of auto
// /\/\/\/\/\ /\
// type of *this ||
// constructor call
}
auto is no good here. Let's try decltype.
A operator +(const A& other) const
{
return decltype(*this)(*this) += other; //error: 'A& A::operator+=(const A&)' discards
// /\/\/\/\/\/\/\ /\ qualifiers [-fpermissive] return
// type of *this || decltype(*this)(*this) += other;
// constructor call ^
}
This managed to get the type out of *this, but operator+ is declared const, so we got const A deduced (that's what I thought). So let's go on:
A operator +(const A& other) const
{
return typename std::remove_const<decltype(*this)>::type(*this) += amount;
//same error as previous
}
Now I got mindblown. Even thought I removed constness, it still discards
qualifier. Well, maybe that's because all I was doing was just CASTING. So stupid. To call a constructor I would have to generate code that (besides type) has ::Constructor (so I even tried to make an alias for constructor, but at this point I failed so hard). My broken brain gave up, but rest of my consciousness gave me an solution (which is generic in writing, so that's good):
// outside of class
template<class A>
inline A&& make_rvalue(A copy)
{
return std::move(copy);
}
// inside of class
A operator +(const A& other) const
{
return make_rvalue(*this) += other; // such generic code
}
That's what I ended with. But is there some trick that doesn't involve any other function call?
EDIT: I know classic methods of doing this, but what I search is described below:
operator is reduced to {return /*...*/;}
doesn't involve names of class methods or global functions
takes to account overloads with other types - it cannot take argument(s) by value, since class A != class B, argument int over const int& doesn't help much with Matrix class (but proxy operator that calls target operator with exchanged arguments is OK)
takes to account (possible) order of operation (x#y != y#x), where both should should have same return statement
return statement should be exacly the same for given operator# is every class that has overloaded operator +=
If you create a function like this:
template <typename T>
T add(T temp,const T &b)
{
temp += b;
return temp;
}
You can use it like this:
A operator+(const A& other) const { return add(*this,other); }
I'm going to go on record as saying this whole line of thinking/coding is going the wrong way. You already know that (in this case) the return type is A. Your code seems to be putting a lot of work into saying A in the most complex, indirect way possible.
A operator +(const A& other) const {
A ret {*this};
ret += other;
return ret;
}
Since you seem to really want to use C++11 features (even though in this case almost none of them provides any real help) I used a braced initializer, so it uses something from C++11. And no, this isn't one line, but it is readable.
Note that the same style is fine in generic code as well. For example, in a non-member overload of operator+, we might have something like this:
template <class T>
T operator+(T const &a, T const &b) {
T ret {a};
ret += b;
return ret;
}
This can be simplified a bit as well though:
template <class T>
T operator+(T ret, t const &b) {
ret += b;
return ret;
}
Again, we lose precisely nothing from the fact that older compilers can and will accept the code without problem.

Overload an operator twice [duplicate]

This question already has answers here:
Overloading by return type
(11 answers)
Closed 8 years ago.
It's possible to overload the same operator twice on C++?
When I try to overload the + operator using the return type as a base, the compiler show me an error.
bigint.h:41:9: error: ‘std::string BigInt::operator+(BigInt)’ cannot be overloaded
bigint.h:40:9: error: with ‘BigInt BigInt::operator+(BigInt)’
This is my code:
.h:
BigInt operator + (BigInt);
string operator + (BigInt);
.cc:
BigInt BigInt::operator + (BigInt M){
if (this->number.size() != M.number.size())
fixLength (this->number, M.number);
// Call Sum;
this->number = Sum (this->number, M.number);
return (*this);
}
string BigInt::operator + (Bigint M){
// Call BigInt overload +;
}
Edit: Apparently I cannot overload the same operator twice using the return type as a base. Suggestions?
As has been pointed out, you cannot overload beased on return type alone. So this is fine:
Foo operator+(const Foo&, const Foo&);
Foo operator+(const char*, double);
but this is not:
Foo operator+(const Foo&, const Foo&);
Bar operator+(const Foo&, const Foo&);
But most of the time there are valid and simple solutions to a given problem. For instance, in a situation like yours, where you want the following to work:
Foo a, b;
Foo c = a + b;
Bar bar = a + b;
then a common strategy is to either give Bar an implicit converting constructor:
struct Bar
{
Bar(const Foo& foo) { .... }
};
or give Foo a conversion operator:
struct Foo
{
explicit operator Bar() { .... }
....
};
Note you can't mark the operator explicit if you don't have a C++11 compiler.
method overload in C++ is by argument list, not the return value.. so in your case, the two methods are ambiguous and the compiler can't tell which one to use (they have the same argument list)
No, you can't overload based on the return type.
From standard docs., Sec 13.1.2,
Function declarations that differ only in the return type cannot be overloaded.
What this means is that methods can be overloading only if they differ by parameters. As with C++, a method's return type is not considered part of the method signature.
Check Wiki for Name Mangling for more details

My explicit instantiation of template class seems doesn't work

I wrote a short program to test the template class's explicit instantiation as follows:
#include <iostream>
template <class T>
struct less_than_comparable {
friend bool operator>=(T const& a, T const& b) {
return !(a < b);
}
};
class Point {
friend bool operator<(Point const& a, Point const& b) {
return a.x_ < b.x_;
}
public:
Point(int x) : x_(x) {}
private:
int x_;
};
template struct less_than_comparable<Point>;
//Here I explicitly instantiate a template class expecting that
//less_han_comparable<Point> would export opeartor>=() for class Point to the global
//namespace, and then the p1 >= p2 statement will work as expected.
int main() {
using namespace std;
Point p1(1), p2(2);
cout << (p1 < p2) << endl;
cout << (p1 >= p2) << endl; //But I have a compiler error here saying that
//no match for ‘operator>=’ in ‘p1 >= p2’
}
I know if I inherit Point from less_than_comparable, the code will pass the compiling.
But my question is why it doesn't work if I use explicit instantiation?
I use G++ 4.4.5 running on Ubuntu 10.04.
Any comments will be appreciated. Thanks.
The problem is that friend functions defined inside class-like types are not injected into enclosing namespace.
The principle you are refering to is called "friend name injection", but this has been replaced in current C++ standard by "ADL" (Argument Dependent Lookup, also called Koenig Lookup). ADL examines all namespaces associated with function parameter types for matching function.
In your case, when you call operator>= in p1 >= p2 (i.e. operator>=(p1, p2);). ADL looks for matching function in the namespace of Point, but Point doesn't have such function.
If you inherit Point from less_than_comparable, operator>= becomes part of namespace of Point and ADL can find it here.
You can check, that no friend name injection takes place here.
The code doesn't work because Point is not a template class where you have defined operator >=.
If you want to compile this code then define operator >= in Point class as well. Note that p1 and p2 are no where related to less_than_comparable.
As side note, why you have defined operator for "greater than equal to" operator in the name of less_than_comparable ?
Explicit instantiation is simply a way to force the compiler to compile a particular template within that translation unit - it has no affect on the lookup of names.
To get what you appear to want, you could for example:
class Point : public less_than_comparable<Point> {
your operator >= is a member function of a completely different type that is unrelated to Point as far as the compiler is concerned. I think what you want to do is:
template< T >
bool operator >= ( T const& a, T const& b ) {... }
forget the class and just make it a global function. And you won't need explicit template instantiation. In fact the only time I've seen it used was when you had template class declared inside a library and used in another project, which you are obviously not doing here.