Re: Dummy streams: eating std::endl?
lindahlb@hotmail.com ha scritto:
#include <iostream>
using namespace std;
struct Ignore_Stream
{
<snip>
template<typename Char, typename Traits>
Ignore_Stream operator<<(basic_ostream<Char,Traits> &
(*)(basic_ostream<Char,Traits> &)) const;
<snip>
};
<snip>
It should work, considering basic_ostream gets it to work (gcc STL uses
a signature almost identical to the signature in the alternate
definition).
In addition to what other posters have already correctly said, I would
like to notice that the signature you think it's "almost" identical is
in fact completely different! Compare the signature in your example
above with this one:
template <class charT, class traits = char_traits<charT> >
class basic_ostream : ...
{
//...
basic_ostream<charT,traits>& operator<<
(basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&));
//...
};
Do you spot the difference? Yours is a member template, while ostream's
is not: it's a regular member (of a class template). That means the
template parameters in ostream's operator<< are not deduced: they *must*
match the object's char and traits. In the case of std::endl, which is a
function template, this fact allows the compiler to select one (and only
one) specialization of the std::endl.
In your case, the compiler needs to deduce the parameters and can't
because there are (potentially) infinite specializations of std::endl
that matches your signature.
Regards,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]