Map with key in another namespace

J

Jim West

Can someone explain to me why the following fails to compile:

#include <map>

namespace LOCAL {
class INDX {
public:
int x, y, z;
};
}

inline bool operator<(const LOCAL::INDX& a, const LOCAL::INDX& b);

typedef std::map<LOCAL::INDX, double> MY_MAP;

int main() {
MY_MAP my_map;
LOCAL::INDX idx, idx1;
MY_MAP::iterator pos = my_map.find(idx);
}


The Intel i386 Linux compiler gives me

/opt/intel/compiler81/include/c++/functional(134): error: no operator
"<" matches these operands
operand types are: const LOCAL::INDX < const LOCAL::INDX
return (_Left < _Right);
^

The GNU G++ error is similar.

Through trial and error, I found that the error goes away if I put
the definition of the < operator into the LOCAL namespace, but I don't
understand why it matters.
 
V

Victor Bazarov

Jim said:
Can someone explain to me why the following fails to compile:

#include <map>

namespace LOCAL {
class INDX {
public:
int x, y, z;
};
}

inline bool operator<(const LOCAL::INDX& a, const LOCAL::INDX& b);

typedef std::map<LOCAL::INDX, double> MY_MAP;

int main() {
MY_MAP my_map;
LOCAL::INDX idx, idx1;
MY_MAP::iterator pos = my_map.find(idx);
}


The Intel i386 Linux compiler gives me

/opt/intel/compiler81/include/c++/functional(134): error: no operator
"<" matches these operands
operand types are: const LOCAL::INDX < const LOCAL::INDX
return (_Left < _Right);
^

The GNU G++ error is similar.

Through trial and error, I found that the error goes away if I put
the definition of the < operator into the LOCAL namespace, but I don't
understand why it matters.

That's how "argument-dependent name lookup" works. If both operands
are in their own namespace, the other namespaces are NOT searched for
the function (in your case 'operator <'). Put your operator in the
same namespace as the class INDX.

Victor
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top