John Gordon
John Gordon

Reputation: 2704

Compiler chooses the wrong operator<<

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;

Upvotes: 3

Views: 176

Answers (3)

The problem is, as one of the comments mentions an Argument Dependent Lookup problem. When the compiler performs lookup for std::cout << my_enum_variable it will look in the current namespace, in the std namespace and the namespace where the type of my_enum_variable is defined. Only if there is no candidate, it will go up and search the enclosing namespaces. In this case, because any enum is implicitly convertible to int it will apply that conversion and not look in enclosing namespaces. The using directive does not help there.

The simple solution is moving the overloaded operator<< into the same namespace that the enumeration:

namespace utcp {
  enum GainType {AGC_GAIN_MODE, MAN_GAIN_MODE };
  inline std::ostream& operator<<(std::ostream &os, const GainType &val) {...}
}

This should enable ADL to pick you overload.

Upvotes: 5

Did you try to explicitly cast your argument to << ?

Upvotes: 1

kfmfe04
kfmfe04

Reputation: 15327

I recommend that you upgrade to at least g++ 4.4 to get some strongly-typed enumeration functionality.

I recently upgraded to 4.6 and changed all my enums to enum class - I believe strong-typing will give you an idea as to the error in the code.

Upvotes: 2

Related Questions