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

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

by Brendan <catphive@[EMAIL PROTECTED] > Apr 25, 2008 at 10:22 AM

Here's a cleaned up version of the code I posted last night for inline
string formatting, which has been actually tested and works. The new
format is

STR_BEGIN << "asdf" << 123 << STR_END

and can be placed in a function like f(STR_BEGIN << "x = " << x <<
STR_END);

You might ask, how is this code type safe? Can't I write:
std::cout << "asdf" << STR_END;

No, you cannot. Give it a try. This is actually my biggest reservation
about this code, because I use an evil macro hack to ensure type
safty. Specifically, there are unbalanced parens in the macro. This
forces STR_BEGIN and STR_END to always appear as a pair, and allows
you to nest them and do things like

std::cout << STR_BEGIN << "asdf" << STR_END;

which would not work right if there were no parens there.

What do you think? Is this too much of a hack? Are there edge cases
I'm missing? Is there a better way?

-Brendan Miller

#include <iostream>
#include <sstream>
#include <string>

template <typename T> class Tem****ary {
   T m_data;
   public:
   T & get() {
    return m_data;
   }
};

struct ToStr {
   static ToStr instance;
};

ToStr ToStr::instance;

#define STR_BEGIN ((Tem****ary<std::ostringstream>().get())
#define STR_END (ToStr::instance))

std::string operator <<(std::ostream& stream, ToStr ts) {
   return (static_cast<std::ostringstream&>(stream)).str();
}

void f(std::string str) {
   std::cout << str;
}

int main(int argc, char** argv) {
   // proper use.
   f(STR_BEGIN << "asdf" << 123 << STR_END);

	return 0;
}

-- 
      [ 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 Tue Jul 8 23:35:44 CDT 2008.