friend vs member for comparison operators

M

Michael Klatt

I've just encountered a strange situation (at least to me) regarding
friend operators and member operators:

#include <map>

class Key
{
friend bool operator<(const Key& lhs, const Key& rhs)
{
return lhs.myInt < rhs.myInt;
}

public :
Key(int i) : myInt(i) {}
bool operator<(const Key& rhs)
{
return myInt < rhs.myInt;
}

private :
int myInt;
};

int main()
{
std::map<Key, int> table; // Key must support operator<()
table.insert(std::make_pair(Key(1), 1));
table.insert(std::make_pair(Key(2), 2));
return 0;
}


I assumed that friend operator<(const Key&, const Key&) and member
operator operator<(const Key& rhs) are effectively the same thing, but
the compiler (g++3) doesn't think so. Without the friend operator I
get an error message like "cannot find match for const Key& < const
Key&" generated within the std::map code, but it does say that Key <
const Key& (the member operator) is a near match. Why can't the
compiler treat a Key as a const Key& in this case?
 
L

lilburne

Michael said:
I assumed that friend operator<(const Key&, const Key&) and member
operator operator<(const Key& rhs) are effectively the same thing, but
the compiler (g++3) doesn't think so. Without the friend operator I
get an error message like "cannot find match for const Key& < const
Key&" generated within the std::map code, but it does say that Key <
const Key& (the member operator) is a near match. Why can't the
compiler treat a Key as a const Key& in this case?

What happens when you make operator<() const?
 
A

Adam Fineman

Michael said:
I've just encountered a strange situation (at least to me) regarding
friend operators and member operators:
The problem is not with friend vs. member functions, but with const vs.
non-const member functions. (See below.)

bool operator<(const Key& rhs)
bool operator<(const Key& rhs) const
{
return myInt < rhs.myInt;
}
Why can't the
compiler treat a Key as a const Key& in this case?

It can, but only if you tell it to.

HTH.

- Adam
 
A

Andrey Tarasevich

Michael said:
I've just encountered a strange situation (at least to me) regarding
friend operators and member operators:

#include <map>

class Key
{
friend bool operator<(const Key& lhs, const Key& rhs)
{
return lhs.myInt < rhs.myInt;
}

public :
Key(int i) : myInt(i) {}
bool operator<(const Key& rhs)
{
return myInt < rhs.myInt;
}

private :
int myInt;
};

int main()
{
std::map<Key, int> table; // Key must support operator<()
table.insert(std::make_pair(Key(1), 1));
table.insert(std::make_pair(Key(2), 2));
return 0;
}


I assumed that friend operator<(const Key&, const Key&) and member
operator operator<(const Key& rhs) are effectively the same thing, but
the compiler (g++3) doesn't think so. Without the friend operator I
get an error message like "cannot find match for const Key& < const
Key&" generated within the std::map code, but it does say that Key <
const Key& (the member operator) is a near match. Why can't the
compiler treat a Key as a const Key& in this case?

According to the error message, the compiler is trying to compare two
constant objects. Your member operator is not declared as 'const', which
means that it cannot be called for a constant left-hand side object.
Declare your member comparison operator as 'const'

...
bool operator<(const Key& rhs) const
...

and the code should compile.
 
M

Michael Klatt

Andrey Tarasevich said:
According to the error message, the compiler is trying to compare two
constant objects. Your member operator is not declared as 'const', which
means that it cannot be called for a constant left-hand side object.
Declare your member comparison operator as 'const'

...
bool operator<(const Key& rhs) const
...

and the code should compile.

Oops, brain cramp, I wasn't thinking straight. Thanks for the suggestion everybody.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top