

|
 |
| Programming > C++ Moderated > Re: How to buil... |
|
| << Topic |
< Post |
Post 16 of 30 Topic 9533 of 9807
|
Post > |
Topic >> |
Re: How to build a string on the fly, using std::ostringstream?
by guinness.tony@[EMAIL PROTECTED]
Apr 28, 2008 at 04:01 PM
| On 23 Apr, 20:14, "Niels Dekker - no return address"
<nore...@[EMAIL PROTECTED]
> wrote:
> Does the Standard Library sup****t creating an std::string by means of
> streaming, without having to declare a *named* ostringstream object? I
tried
> the following:
>
> #include <sstream>
> #include <string>
> int i = 42;
>
> std::string s = (std::ostringstream() << "i = " << i).str();
>
> Of course it didn't compile, because a tem****ary (std::ostringstream())
cannot
> be passed as non-const reference to a function (operator<<).
This is odd. The operator<< that is called here...
> std::string s = (std::ostringstream() << "i = " << i).str();
^^
is member function std::ostream::operator<<(char const*). There is no
case of a tem****ary being bound to a non-const reference here. It is
perfectly legal and should compile just fine. What compiler are you
using
that complains here (without the .str() business)?
The problem you describe would occur if the argument to operator<<
were not
catered-for by the overloaded set of member functions in
std::basic_ostream
(e.g. if it were a std::string or std::complex<T>.) There is a well-
known
idiom for cir***venting this problem (without inventing your wrapper
class)
by using the knowledge that the member-function versions of operator<<
all
return lvalue references; one such member is the one that executes
free
functions whose signatures resemble "std::ostream&
func(std::ostream&)"
as if they were manipulators in the operator<< chain (e.g. std::endl,
std::flush.) The idiomatic approach is to select std::flush for this
job as it has no discernible side-effects; thus:
int i = 42;
std::string prefix = "i = ";
std::ostringstream() << std::flush << prefix << i;
> Okay, second try, wrapping the ostringstream as follows:
>
> template <typename T> class Wrapper {
> T m_data;
> public:
> T & get() {
> return m_data;
> }
> };
>
> std::string s = (Wrapper<std::ostringstream>().get() << "i = " <<
i).str();
As explained above, all this is unnecessary. You should still be
addressing
the single problem in your code as originally presented.
> Unfortunately it still didn't compile, because operator<< returns an
> std::basic_ostream, instead of an std::ostringstream, and therefore it
doesn't
> have an str() member function. Now I wonder, is it safe to cast the
stream,
> returned by operator<< to an ostringstream reference?
Yes, indeed. And since you *know* that the actual object (to whom the
ostream reference refers) is definitely an ostringstream, you can use
static_cast rather than dynamic_cast:
int i = 42;
std::string s = static_cast<std::ostringstream&>(
std::ostringstream() << "i = " << i).str();
> At least, it *seems* to work... But is it the proper way to do it?
Yes.
And if you need to cope with e.g. string objects as the first
item in the operator<< chain, you can also employ the temp-to-lvalue
conversion outlined above, e.g.
int i = 42;
std::string prefix = "i = ";
std::string s = static_cast<std::ostringstream&>(
std::ostringstream() << std::flush << prefix << i).str();
Regards,
Tony.
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|
30 Posts in Topic:
|
"Niels Dekker - no r |
2008-04-23 13:14:37 |
|
Martin York <Martin.Yo |
2008-04-24 01:05:54 |
|
Peter Jones <dev-null@ |
2008-04-24 01:10:33 |
|
Carl Barron <cbarron41 |
2008-04-24 01:43:21 |
|
Vidar Hasfjord <vattil |
2008-04-24 15:12:53 |
|
Brendan <catphive@[EMA |
2008-04-24 15:43:20 |
|
Brendan <catphive@[EMA |
2008-04-25 10:22:59 |
|
Niels Dekker - no return |
2008-04-26 08:46:24 |
|
Carl Barron <cbarron41 |
2008-04-27 07:13:20 |
|
Brendan Miller <catphi |
2008-04-27 07:29:25 |
|
Vidar Hasfjord <vattil |
2008-04-27 07:29:42 |
|
Vidar Hasfjord <vattil |
2008-04-28 01:56:19 |
|
Vidar Hasfjord <vattil |
2008-04-28 02:13:22 |
|
vova777@[EMAIL PROTECTED] |
2008-04-28 16:06:27 |
|
"Niels Dekker - no r |
2008-04-28 16:01:35 |
|
guinness.tony@[EMAIL PROT |
2008-04-28 16:01:50 |
|
"Hendrik Schober&quo |
2008-04-28 20:35:47 |
|
Vidar Hasfjord <vattil |
2008-04-29 10:38:48 |
|
Brendan Miller <catphi |
2008-04-29 10:43:23 |
|
Vidar Hasfjord <vattil |
2008-04-29 10:43:23 |
|
Vidar Hasfjord <vattil |
2008-04-29 10:43:22 |
|
vova777@[EMAIL PROTECTED] |
2008-04-29 12:13:56 |
|
Vidar Hasfjord <vattil |
2008-04-29 17:41:45 |
|
Niels Dekker - no return |
2008-04-30 10:26:51 |
|
vova777@[EMAIL PROTECTED] |
2008-04-30 10:39:18 |
|
Vidar Hasfjord <vattil |
2008-04-30 17:04:43 |
|
guinness.tony@[EMAIL PROT |
2008-05-01 13:12:58 |
|
"Niels Dekker - no r |
2008-05-02 12:45:30 |
|
guinness.tony@[EMAIL PROT |
2008-05-07 11:43:29 |
|
"Niels Dekker - no r |
2008-05-08 21:29:05 |
|
Post A Reply:

|
|
|
|