Ambiguous operator<< overloading in Clang

Viewed 86

The following code does not compile in clang 3.5+, but compiles in all gcc versions and clang < 3.5.

struct Logger {
    template <class T>
    Logger& operator<<(const T& /*val*/)
    {
        // m_buf << val;
        return *this;
    }
};

struct Value {
    template <class Stream>
    friend Stream& operator<<(Stream& os, const Value& /*val*/)
    {
        // os << m_val;
        return os;
    }
};

int main()
{
    Logger logger;
    Value value;
    logger << value;
    return 0;
}

The clang error is

<source>:23:12: error: use of overloaded operator '<<' is ambiguous (with operand types 'Logger' and 'Value')

    logger << value;

    ~~~~~~ ^  ~~~~~

<source>:3:13: note: candidate function [with T = Value]

    Logger& operator<<(const T& /*val*/)

            ^

<source>:12:20: note: candidate function [with Stream = Logger]

    friend Stream& operator<<(Stream& os, const Value& /*val*/)

                   ^

Who is correct according to the standard, clang or gcc? If clang, how do I properly write a logger with "<<" that redirects any value supporting "<<" to another stream?

0 Answers
Related