c++ - How can I resolve this address of overloaded member function issue? -
[edit - long since forgot here until got 2,500 views "notable question". since people viewing - there useful information overloads in accepted answer, checking std::endl
worse realized @ time, , wrong thing.
basically, effect of std::endl
output \n
stream flush std::flush
. that's irrespective of platform, including windows end-of-line "\r\n". endl
manipulator doesn't abstract away platform differences wrt line ends, c++ handles same way c - translating \n
"\r\n" (for text mode, not binary) later on. thought c++ doing different, assumption strong never questioned 2 decades, wrong.
i don't remember details, it's possible define own streams anyway, , provide alternative output (and translation) of whatever characters streamed in. manipulators should work expected, before custom stream code sees resulting output characters. provide special end-of-line behaviour, watch \n
there (which still before text-file end-of-line translation). ]
it's hackish, know, needed implement stream class act standard stream, detect std::endl manipulator , special-case it's behaviour. first attempt @ particular method implementation was...
mystream& mystream::operator<< (std::basic_ostream<char>& (*p) (std::basic_ostream<char>&)) { if (p == &std::endl) { // handle special case } else { m_underlying_stream << p; } return *this; }
the trouble compiler doesn't know overload of std::endl
i'm referring to. resolved follows...
mystream& mystream::operator<< (std::basic_ostream<char>& (*p) (std::basic_ostream<char>&)) { typedef std::basic_ostream<char>& (*endl_t) (std::basic_ostream<char>&); const endl_t l_endl (&std::endl); if (p == l_endl) { // handle special case } else { m_underlying_stream << p; } return *this; }
that compiler can resolve overload in context of initialisation (and assignment too, experiment proved), not operator==
.
the compiler in question mingw gcc 4.4.0, don't think compiler issue.
i had around , found question...
how address of overloaded member function?
if code has const issue, don't know missing const needs go. can't see other obvious type issue.
i have vague ideas number-of-steps issues wrt overloading or implicit casting, nothing concrete. - can explain wrong first example, why second version fixes it, , how can safely indicate overload mean when taking address of function.
btw - can guess people won't me testing directly address of std::endl
, , point out fragile - e.g. have own manipulator calls std::endl
wouldn't spot. in general true, in special case, hack saves lot of time , nastiness doesn't matter.
the use of overloaded function name, (or name of function template behaves set of overloaded functions) without arguments (such in "address of" expression) allowed in limited set of contexts context can used uniquely determine particular overload required.
this specified in 13.4 of standard (iso/iec 14882:2003) [over.over]. included initializer object or reference or in explicit conversion. gives number of options.
e.g. explicit conversion:
typedef std::ostream& (*manipptr)(std::ostream&); mystream& mystream::operator<<(manipptr p) { if (p == static_cast<manipptr>(&std::endl)) { // ...
directly initializing pointer:
typedef std::ostream& (*manipptr)(std::ostream&); mystream& mystream::operator<<(manipptr p) { const manipptr pendl = &std::endl; if (p == pendl) { // ...
Comments
Post a Comment