C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: std::ends useful?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ron Natalie
Guest





PostPosted: Fri Aug 15, 2003 9:25 pm    Post subject: Re: std::ends useful? Reply with quote




"Saul" <stamari (AT) bigfoot (DOT) com> wrote


Quote:
Is std::ends useful in any way (not considering strstream) ?

Well, if you need a way to put a null into a stream it is. Of course, outside
of strsteram, there's no overriding need to put a null into stream (and you
could
always output a '', ends doesn't even do a flush like endl does).



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Mon Aug 18, 2003 10:37 am    Post subject: Re: std::ends useful? Reply with quote



Hi,

Saul wrote:

Quote:
Is std::ends useful in any way (not considering strstream) ?

It all depends on what you expect on the other "side" of the stream.
Let's suppose that the stream is wrapped around a network socket and
that the application on the other side (here, on the other side of the
wire) expects that every string is zero-terminated. This is reasonable
and I bet that there are many servers working this way.

std::ends can be useful to explicitly inject zeros into the stream.
Of course, you can also do this by:

s << '';

--
Maciej Sobczak
http://www.maciejsobczak.com/

Distributed programming lib for C, C++, Python & Tcl:
http://www.maciejsobczak.com/prog/yami/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Spangenberg
Guest





PostPosted: Mon Aug 18, 2003 4:37 pm    Post subject: Re: std::ends useful? Reply with quote





Maciej Sobczak schrieb:
[snip9

Quote:
std::ends can be useful to explicitly inject zeros into the stream.
Of course, you can also do this by:

s << '';


One advantage of ends versus '' is the fact, that you can use ends
more easily in generic code, which doesn't know its character type.
(Otherwise you have to define a typedef for the character type and
could use char_type() instead).

Yours,

Daniel


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Aug 18, 2003 11:49 pm    Post subject: Re: std::ends useful? Reply with quote

In article <bhoea4$s4f$1 (AT) nemesis (DOT) news.tpi.pl>, Maciej Sobczak
<maciej (AT) maciejsobczak (DOT) com> writes
Quote:
Hi,

Saul wrote:

Is std::ends useful in any way (not considering strstream) ?

It all depends on what you expect on the other "side" of the stream.
Let's suppose that the stream is wrapped around a network socket and
that the application on the other side (here, on the other side of the
wire) expects that every string is zero-terminated. This is reasonable
and I bet that there are many servers working this way.

std::ends can be useful to explicitly inject zeros into the stream.
Of course, you can also do this by:

s << '';

but ends avoids the use of a magic number. However I wish it had been
differently named such as end_str.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Tue Aug 19, 2003 11:28 pm    Post subject: Re: std::ends useful? Reply with quote


"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
Quote:

std::ends can be useful to explicitly inject zeros into the stream.
Of course, you can also do this by:

s << '';

but ends avoids the use of a magic number. However I wish it had been
differently named such as end_str.

There's no "magic number." A null character is always , it's no more
magic than n is a newline.

The only real compelling argument I've heard is that ends works independent
of the char type of the streams. Otherwise you potentially need to use
L'' or some other value.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Wed Aug 20, 2003 1:59 pm    Post subject: Re: std::ends useful? Reply with quote

Hi,

Ron Natalie wrote:

Quote:
The only real compelling argument I've heard is that ends works independent
of the char type of the streams. Otherwise you potentially need to use
L'' or some other value.

The argument behind ends being independent is much more compelling. Wink
It is not only independent, it is *generic*.

Consider:

template <class Stream>
void endAndFlush(Stream &s)
{
s << ends << flush;
}

This code works with all kinds of streams and is suitable for any
stream-like being without changes. When you use (more or less) magic
numbers like '' or L'' (or some other value), you always bind your
code to the particular type of character used in the stream. With ends
the code is not bound to anything and the compiler will happily resolve
it to what is actually appropriate.

In this context, '', L'', 'n', L'n', etc. *are* magic, because
they have fixed target "platform" (the type of stream).

In contrast, the manipulators ends, endl, flush, etc. are not magic,
because they are not bound to any particular type of stream.

--
Maciej Sobczak
http://www.maciejsobczak.com/

Distributed programming lib for C, C++, Python & Tcl:
http://www.maciejsobczak.com/prog/yami/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bo Persson
Guest





PostPosted: Thu Aug 21, 2003 10:52 am    Post subject: Re: std::ends useful? Reply with quote


"Ron Natalie" <ron (AT) sensor (DOT) com> skrev i meddelandet
news:3f423226$0$153$9a6e19ea (AT) news (DOT) newshosting.com...
Quote:

"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message

std::ends can be useful to explicitly inject zeros into the
stream.
Of course, you can also do this by:

s << '';

but ends avoids the use of a magic number. However I wish it had
been
differently named such as end_str.

There's no "magic number." A null character is always , it's no
more
magic than n is a newline.

The only real compelling argument I've heard is that ends works
independent
of the char type of the streams. Otherwise you potentially need to
use
L'' or some other value.

Maybe not. All streams inheriting from std::basic_ostream would have
this inserter

template basic_ostream<_CharT, _TraitsT>&
operator<<(basic_ostream<_CharT, _TraitsT>& _Stream, char
_Char);


which will supposedly widen the char as needed.


Bo Persson
[email]bop2 (AT) telia (DOT) com[/email]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.