https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64611
Bug ID: 64611 Summary: Using a << operator inside an overloaded << operator gives compile error Product: gcc Version: 4.8.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jvoosten at bankai dot nl Created attachment 34453 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=34453&action=edit Preprocessed file The following code gives a compile error on the indicated line: #include <sstream> class SmartStream { public: template <typename F> friend F &operator << (F &in, float f) { in.type ("f"); in.m_buf << f; // This causes a 'recursive' compile call return in; } protected: std::ostringstream m_buf; void type(const std::string &_type) {}; }; int main(int argc, char *argv[]) { SmartStream ss; ss << 123.456; } Compile output: $ g++ -save-temps -c rec_stream.cpp -o rec_stream.o rec_stream.cpp: In instantiation of 'F& operator<<(F&, float) [with F = std::basic_ostringstream<char>]': rec_stream.cpp:10:18: required from 'F& operator<<(F&, float) [with F = SmartStream]' rec_stream.cpp:24:11: required from here rec_stream.cpp:9:9: error: 'class std::basic_ostringstream<char>' has no member named 'type' in.type ("f"); ^ rec_stream.cpp:10:18: error: 'class std::basic_ostringstream<char>' has no member named 'm_buf' in.m_buf << f; // This causes a 'recursive' compile call ^ Basically, it's applying the friend operator << function on the internal std::ostringstream object, while I would expect the compiler to pick 'std::ostringstream::operator <<()'. Furthermore, casting, using :: resolution operators, using the 'using' word all does not work. In fact, even the following still triggers the error: template <typename G> std::string xyz(std::ostringstream &buf, G _param) { buf.str(""); buf << _param; return buf.str(); } ..and replacing the offending line with xyz<float>(m_buf, f); Only if I move the code completely outside of SmartStream in a separate function it will compile. I also posted this on StackOverflow (http://stackoverflow.com/questions/27952591/using-a-operator-inside-an-overloaded-operator) and apparently this compiles on clang++. Perhaps there's an ambiguity towards which 'operator <<' must be applied, but then I'd expect to be able to specify which one.