Incorrect overload selected for flow manipulator

Here is the code:

#include <iostream> #include <iomanip> #include <typeinfo> #if 0 std::ostream &foo(std::ostream &os, std::ios_base &(*x)(std::ios_base &), bool show_id = false) { if ( show_id ) os << "(" << typeid(x).name() << ") "; return os << x; } #endif template<typename T> std::ostream &foo(std::ostream &os, T const &t, bool show_id = false) { if ( show_id ) os << "(" << typeid(t).name() << ") "; return os << t; } int main() { foo(std::cout, std::hex) << 255 << std::endl; foo(std::cout, ".") << std::hex << 255 << std::dec << std::endl; foo(std::cout, std::hex, true) << 255 << std::endl; } 

Using bcc32 6.70 and bcc32 5.82, exit

 401358255 .ff (std::ios_base & (*)(std::ios_base &) const) 401368255 

Using bcc64 6.70 (based on clang) and g ++ 4.8.2, output

 ff .ff (FRSt8ios_baseS0_E) ff 

I assume that clang and gcc are true because they have a better reputation than bcc32.

If I enable the commenting function, then the bcc32 outputs:

 ff .ff (std::ios_base & (*)(std::ios_base &)) ff 

What exactly happens with the first version? This is probably a compiler error related to overload resolution, but I can't figure out what bcc32 does or what const is at the end of typeid output.

+1
c ++ operator-overloading overload-resolution c ++ builder c ++ builder-xe6
source share

No one has answered this question yet.

See similar questions:

8
Why does std :: endl generate this cryptic error message?

or similar:

1994
What are the basic rules and idioms for operator overloading?
343
Why doesn't Java offer operator overloading?
280
How to overload operator [] in C #
276
How to overload __init__ method based on argument type?
198
How to correctly overload the << operator for a stream?
5
std :: atomic_is_lock_free (shared_ptr <T> *) does not compile
3
Output stream as a class member
2
Bad memory access with strcat
one
Eclipse C ++ for Mac settings (<iostream> fix) and error correction
0
Overload << to define manipulators

All Articles