This is the mail archive of the libstdc++@sources.redhat.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: About endl...


Jean-Marc Valin <valj01@gel.usherb.ca> writes:
> > <ostream> declares endl to be:
> > 
> >   // 27.6.2.7 Standard basic_ostream manipulators
> >   template<typename _CharT, typename _Traits>
> >     basic_ostream<_CharT, _Traits>&
> >     endl(basic_ostream<_CharT, _Traits>& __os)
> >     { return flush(__os.put(__os.widen('\n'))); }
> 
> I saw that in the headers, but I don't understand why is breaks the
> original function I posted:

'endl' is a template.  So the name 'endl' per se does not have a type.

> template <class T>
> void output(T val) {
>    cout << val;
> }
> 
> Is there a workaround? How can I implement an (template) output
> method that could accept endl as argument. 

A simple workaround maybe to invoke output as

  output(endl<char>);

since 'endl<char>' is a function, and has a type.

So, for your example, you need to do

  streamWrapper << endl<char>;

instead of

  streamWrapper << endl;

The real way to go would be to make your wrapper a streambuf instead.
The iostream interface is not designed to be inherited from nor to be
wrapped.

For more details, look at various postings about iostreams by Dietmar
Kuehl, and the book "Standard C++ IOStreams and Locales" by Angelika
Langer and Klaus Kreft, ISBN 0-201-18395-1.

> Also, from the header definition, I don't understand why
> typeid(endl) doesn't work either. Can anyone enlighten me?

For the reason I said above -- the name 'endl' is a template and
doesn't have a type.  You must "instantiate" the template to get a
function on which 'typeid' can be called (i.e., typeid(endl<char>)).

- Hari
-- 
Raja R Harinath ------------------------------ harinath@cs.umn.edu
"When all else fails, read the instructions."      -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing."   -- Roy L Ash

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]