Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: How to buil...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 28 of 30 Topic 9533 of 9823
Post > Topic >>

Re: How to build a string on the fly, using std::ostringstream?

by "Niels Dekker - no return address" <noreply@[EMAIL PROTECTED] > May 2, 2008 at 12:45 PM

Tony (aka "Guinness Tony") wrote:
> The C-style string insertion operators are, indeed,
> defined as free function templates in [lib.ostream]27.6.2.1:
>
> namespace std {
>   ...
>   template<class charT, class traits>
>   basic_ostream<charT,traits>&
> operator<<(basic_ostream<charT,traits>&,
>                                       const charT*);
>   template<class charT, class traits>
>   basic_ostream<charT,traits>&
> operator<<(basic_ostream<charT,traits>&,
>                                       const char*);
>   // partial specializationss
>   template<class traits>
>   basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
   ...
>
> [how odd - I've never noticed that "specializationss" typo before!]

Actually those overloaded operator<< functions aren't even template
specializations! In 2001 it was already noticed by Andy Sawyer: LWG issue
311
"Incorrect wording in basic_ostream class synopsis"
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#311


> Sorry to have added unnecessary confusion to the discussion.

No problem.  I was mistaken as well, when I wrote:
>>
>> 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<<).

Only after seeing the reply by Vidar Hasfjord, I double-checked, and found
that my toolset actually accepted inserting a literal string onto a
tem****ary
stream, because it had offered that specific operator<< as a member
function!!! (I wasn't using STL****t.)


> Nevertheless, your example still compiles under the toolsets I have
> to hand...
>
>     int i = 42;
>     std::string s = static_cast<std::ostringstream&>(
>         std::ostringstream() << "i = " << i).str();
>
> ...but, on execution, I find that the operator<< chosen by the
> compiler is std::ostream::operator<<(const void*), which /is/ a
> member function (I checked this time!)  The text written into
> the stringstream's string buffer is not a copy of the string
> literal, but a representation of /the address of/ that string
> literal.

Interesting!!!  That could be a source of very nasty bugs, especially when
switching from one library implementation to another!


> In conclusion, the "flush trick" I presented in my previous post is
> also required when the first item in the insert-chain is a C-style
> string (including string literals), i.e.
>
>     int i = 42;
>     std::string s = static_cast<std::ostringstream&>(
>         std::ostringstream() << std::flush << "i = " << i).str();

Still, why do you think the "flush trick" is superior to creating a
"tem****ary" lvalue by means of a wrapper? Do you have any references,
showing
that the "flush trick" is indeed a well-known idiom?  (I wonder how many
people actually /know/ that std::flush is inserted by a member function,
while
other objects, e.g., strings, are inserted by non-member functions.)
Otherwise I think I'll stick to the wrapper...


Kind regards, Niels


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




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

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 1:33:16 CDT 2008.