I have two libraries, one of which depends on the other. The first library defines some enumerations and provides an operator<<() that prints a short chunk of text for each enumeration. I have unit tests proving that this works.
The second library, which depends on the first, passes an enumeration value to std::cout (std::ostringstream behaves the same) and gets the corresponding numerical value rather than the text. No templated code is involved so it’s not a template vs overload issue. I’ve looked through the preprocessor output to verify that the correct definitions are visible. I’m using g++ 4.1.2.
I’m unable to reproduce the problem outside of my libraries (which are thousands of lines), so I can’t post any example code. My attempt to produce example code also convinces me that I understand how this is supposed to work. I’m guessing that something included in one of the headers is causing the compiler to make a different choice when selecting which operator<< to use.
My Question: How can I get insight into what choices the compiler has in choosing which version of the operator to use and why it picked the standard one over mine.
Edit: Adding signatures as requested:
Note though that this simplified example does not exhibit the problem.
Header from the first library:
namespace utcp {
enum GainType {AGC_GAIN_MODE, MAN_GAIN_MODE };
}
// I've tried this passing the argument by value as well.
inline std::ostream& operator<<(std::ostream &os, const hdr::GainType &val)
{...}
Code from the second libary
std::cout << "Gain Text: " << hdr::AGC_GAIN_MODE << std::endl;
The problem is, as one of the comments mentions an Argument Dependent Lookup problem. When the compiler performs lookup for
std::cout << my_enum_variableit will look in the current namespace, in thestdnamespace and the namespace where the type ofmy_enum_variableis defined. Only if there is no candidate, it will go up and search the enclosing namespaces. In this case, because anyenumis implicitly convertible tointit will apply that conversion and not look in enclosing namespaces. Theusingdirective does not help there.The simple solution is moving the overloaded
operator<<into the same namespace that the enumeration:This should enable ADL to pick you overload.