Topic: Re: Why std::string doesn t include auto= matic conv?= =?windows-1252?Q?ersion from the standard primitives?
Author: Mathias Gaunard <loufoque@gmail.com>
Date: Tue, 6 Jul 2010 13:06:30 CST Raw View
On Jul 6, 9:30 am, Peng Yu <pengyu...@gmail.com> wrote:
> In order to convert an int to a string, I have to write the following.
>
> #include <sstream>
>
> int i = 5;
> std::string s;
> std::stringstream out;
> out << i;
> s = out.str();
>
> I'm wondering why std::string doesn=92t include automatic conversion
> from the standard primitives.
>
> For example, I won't be much concise to write the following than the
> above 5 lines of code. Can I suggest to add this in future C++
> standard? Where the suggestion should be sent to?
>
> std::string foo = 123
That's bad design.
A class should be as minimal as possible, plus that wouldn't allow any
user-defined type to be convertible to a string, and putting that for
each "important" type in std::string adds very high coupling.
--
[ 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<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Tue, 6 Jul 2010 13:05:44 CST Raw View
On Jul 6, 10:30 am, Peng Yu <pengyu...@gmail.com> wrote:
> In order to convert an int to a string, I have to write the following.
>
> #include <sstream>
>
> int i = 5;
> std::string s;
> std::stringstream out;
> out << i;
> s = out.str();
Yeah, this is ugly, therefore C++0x will add some convenience
functions. With these function above code could be simplified to:
#include <string>
int i = 5;
std::string s = std:.to_string(i);
> I'm wondering why std::string doesn=92t include automatic conversion
> from the standard primitives.
There exists no general agreement on whether this would
be a good thing or a bad thing for std::string. Personally
I consider your suggestion as not such a good advice,
because it gives the built-in types an extraordinary role
compared to user-defined types. The current IO layer
cleanly separates streaming from the actual types.
It allows that any user-defined type can participate
equally to built-in types in IO.
Adding all sorts of conversions as an non-extendable
set to a type emphasizes even more the differences.
Even worse, adding them directly as std::string
members would increase once more the already
much too large set of member functions of this
type. Herb Sutter wrote once a nice article about
this problem, see:
http://www.gotw.ca/gotw/084.htm
Therefore the currently suggested fix for the current
lack of a simple conversion is a set of function
overloads that provides this conversion. These
functions are just provided as convenience
functions and if more advanced formatting
options are required, you will need to use
more advanced tools anyway.
> Can I suggest to add this in future C++ standard?
Sure.
> Where the suggestion should be sent to?
Actually this newsgroup is a very reasonable
destination.
HTH & Greetings from Bremen,
Daniel Kr=FCgler
--
[ 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<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Gennaro Prota <gennaro.prota@yahoo.com>
Date: Wed, 7 Jul 2010 11:13:23 CST Raw View
On 06/07/2010 21.05, =?ISO-8859-1?Q?Daniel_Kr=FCgler?= wrote:
[...]
> Can I suggest to add this in future C++ standard?
>>
>
> Sure.
>
> Where the suggestion should be sent to?
>>
>
> Actually this newsgroup is a very reasonable
> destination.
>
Actually it has never been, unless you take "suggestion" in a
very mild sense. You can discuss your "suggestions" with those
willing to discuss them, and that's pretty much it.
(I'm sure Daniel actually knows this, but I was under the
impression that the OP was asking for an "official", or at least
effective, channel to propose changes or additions. In practice,
nothing can be effective if --at the very least-- you don't
produce a specification: just telling you want conversion
functions isn't enough.)
--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
[ 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<std-c%2B%2B@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: Sat, 10 Jul 2010 01:56:06 CST Raw View
On 7 Jul., 19:13, Gennaro Prota <gennaro.pr...@yahoo.com> wrote:
> On 06/07/2010 21.05, =?ISO-8859-1?Q?Daniel_Kr=FCgler?= wrote:
> [...]
>
> > Can I suggest to add this in future C++ standard?
>
> > Sure.
>
> > Where the suggestion should be sent to?
>
> > Actually this newsgroup is a very reasonable
> > destination.
>
> Actually it has never been, unless you take "suggestion" in a
> very mild sense. You can discuss your "suggestions" with those
> willing to discuss them, and that's pretty much it.
>
> (I'm sure Daniel actually knows this, but I was under the
> impression that the OP was asking for an "official", or at least
> effective, channel to propose changes or additions. In practice,
> nothing can be effective if --at the very least-- you don't
> produce a specification: just telling you want conversion
> functions isn't enough.)
I understood the OP as you did. Nevertheless it is generally
recommended that someone who wants to make an official
proposal to the C++ committee should start that via a posting
to c.s.c++.
If the OP has a full-fledged proposal and doesn't believe that
a "test" posting of this proposal in this newsgroup would change
his/her mind, (s)he should send this proposal to the corresponding
chair of the working group. For core language suggestions this is
the e-mail address given on top of
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html
and for library suggestions this is the e-mail address given on
top of:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html
Generally recommended formats are html or pdf nowadays.
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 ]