Topic: ostream_iterator should have a ctor that takes a basic_string


Author: Steve Ward <planet36@gmail.com>
Date: Mon, 31 Aug 2009 09:49:50 CST
Raw View
Referencing n2914.pdf

24.6.2.1 ostream_iterator constructors and destructor
[ostream.iterator.cons.des]

ostream_iterator(ostream_type& s, const charT* delimiter);
Effects: Initializes out_stream with s and delim with delimiter.

There should be an overloaded constructor that takes a basic_string as
the delimiter.

example:
ostream_iterator(ostream_type& s, const basic_string<charT, traits>&
delimiter);



Steve

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Mon, 31 Aug 2009 12:37:31 CST
Raw View
On 31 Aug., 17:49, Steve Ward <plane...@gmail.com> wrote:
> Referencing n2914.pdf
>
> 24.6.2.1 ostream_iterator constructors and destructor
> [ostream.iterator.cons.des]
>
> ostream_iterator(ostream_type& s, const charT* delimiter);
> Effects: Initializes out_stream with s and delim with delimiter.
>
> There should be an overloaded constructor that takes a basic_string as
> the delimiter.
>
> example:
> ostream_iterator(ostream_type& s, const basic_string<charT, traits>&
> delimiter);

I disagree. ostream_iterator is a lightweight container of the
delimiter as indicated by the const char* member (for exposition
only), therefore just holding a pointer to the delimiter sequence.

If your suggested constructor would be added, this would
either completely change the current ownership responsibility
and would cause ostream_iterator to become a possibly fat
container or this would make usage of the class very brittle,
because during the usage time of ostream_iterator the
string must neither leave the scope nor must it be modified,
such that the ostream_iterator object can just point to the
result of delimiter.c_str() (assuming the library already
does not do any reference counting, which is allowed in
C++03, but has been invalidated for C++0x).

If you have the requirements of a dynamically determined
delimiter, you should provider a helper class that contains
both the basic_string and the ostream_iterator. Then you
can guarantee that after some initialization of the string,
the ostream_iterator can be created with the result of
a c_str() call of the string and you must not modify the
string after this construction until the iterator is destroyed
again.

HTH & Greetings from Bremen,

Daniel Kr   gler



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]