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 19 of 30 Topic 9533 of 9831
Post > Topic >>

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

by Brendan Miller <catphive@[EMAIL PROTECTED] > Apr 29, 2008 at 10:43 AM

On Mon, 28 Apr 2008 01:56:19 CST, Vidar Hasfjord wrote:

> On Apr 27, 2:29 pm, Brendan Miller <catph...@[EMAIL PROTECTED]
> wrote:
>>>>       using ::operator <<; // Note: Ugly disambiguation.
>> Is this using statement really necessary? What ambiguity exists?
> 
> See my reply to Niels. I was unclear on the reason for the using
> statement. As you point out its only use is for disambiguation. The
> global namespace is of course always searched, but with the using
> declaration any matching global operator becomes the preferred.
> 
> I have since removed the using statement because I found better
> solutions in the client code were lookup problems occurred.
> 
>>>> Usage:
>>>>   string s = S () << "i = " << i;
>>
>>> Cool!!!  It looks much better than my approach!
>>
>> Yes and much better than mine, which I was proud of up until now. This
>> strikes me as the One True Way of inline stringbuilding.
> 
> In the search for the ultimate terseness:
> 
>    const struct StringBuilderFactory
>    {
>      struct StringBuilder
>      {
>        std::auto_ptr <std::ostringstream> s;

Well, if you are confident that RVO is performed, you can get rid of the
auto_ptr and write the copy constructor as
StringBuilder(const StringBuilder& sb);

with no body. However, this will fail if your compiler isn't very
agressive
about RVO. In particular, in debug mode of VC9, you get a linker error,
but
release mode it links fine. I've seen this trick pulled in GCC to some
effect.

By default I think that the auto_ptr is fine, and definitely more
****table,
but if you need to do some optimizing and your compiler sup****ts RVO
(recent GCC and VC++ do if optimization is on), you can use this little
hack to avoid dynamic memory allocation. However, I doubt that the
auto_ptr
here will ever become so expensive that it is worth the hack to avoid it.

> 
>        StringBuilder ()
>          : s (new std::ostringstream ())
>        {}
> 
>        template <typename T>
>        inline StringBuilder& operator << (const T& v)
>        {
>          *s << v;
>          return *this;
>        }
> 
>        inline operator std::string () const
>        {return s->str ();}
>      };
> 
>      template <typename T>
>      StringBuilder operator << (const T& obj) const
>      {return StringBuilder () << obj;}
>    }
>    S;
> 
> Usage:
> 
>    string s = S << "i = " << i;

If I try:
S << "asdf" << endl;

For some reason it can't find an overload for << endl.

-- 
      [ 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 Sat Jul 26 2:41:57 CDT 2008.