Click here to Skip to main content
15,178,008 members
Articles / Mobile Apps / Windows Mobile
Posted 16 Dec 2002


22 bookmarked

Quick and convenient way to build STL strings.

Rate me:
Please Sign up or sign in to vote.
4.00/5 (17 votes)
9 Jul 2013CPOL1 min read
A quick and convenient way to build STL strings.


This is probably one of the shortest articles you'll ever see on Code Project but the one line (!) of code presented here I find so insanely handy, I thought that I'd share it with y'all. That, and I wanted to get my article count up a bit :-)

stringstream's are really useful for building strings. No more sprintf buffer overruns or tediously trying to match up parameters with their placeholders in the format string. As an example, this is how you would use one:

#include <sstream>

int theAnswer = 42 ;

// build the string 
std::ostringstream buf ; 
buf << "The answer is: " << theAnswer ;
std::string str = buf.str() ;

cout << "My string = " << str << endl ; 

But I find it tedious to create the stringstream variables all the time, especially if there are several of them in the same scope since you have to give each one a unique name. So I wrote the following macro:

#define MAKE_STRING( msg )  ( ((std::ostringstream&)(std::ostringstream() 
    << msg)).str() )

It creates a temporary stringstream object, streams in the message and returns the string buffer when it's done. Note that the cast is necessary since operator<<() returns a reference to an ostream but we need an ostringstream to get at the underlying string buffer. You would use it like this:

std::string str = MAKE_STRING( "The answer is: " << theAnswer ) ;

It becomes even handier when you only want a temporay string object e.g.

extern void processString( const std::string& ) ;

processString( MAKE_STRING( "The answer is: " << theAnswer ) ) ;

I also have a version that returns the C-style char* pointer if that's what you need:

#define MAKE_CSTRING( msg )  ( ((std::ostringstream&)
    (std::ostringstream() << msg)).str().c_str() )

char buf[ 1024 ] ; 
strcpy( buf , MAKE_CSTRING("The answer is: " << theAnswer) ) ;

The only thing you need to watch here is that the char* pointer returned is a pointer to a temporary object. So in the example above, it's OK since the string object will stay alive until the call to strcpy() returns. But this is wrong:

const char* pDontTryThisAtHome = MAKE_CSTRING( 
    "The answer is : " << theAnswer ) ; 
printf( "%s\n" , pDontTryThisAtHome ) ;


And that's it! Article count: 2.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Taka Muraoka
Australia Australia
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
tnagata205010-Jul-13 15:35
Membertnagata205010-Jul-13 15:35 
GeneralMemory address being printed Pin
an_phu28-Oct-04 9:32
Memberan_phu28-Oct-04 9:32 
AnswerRe: Memory address being printed [modified] Pin
ashkira5-Jul-09 23:27
Memberashkira5-Jul-09 23:27 
GeneralWord of Caution Pin
Michael Geddes23-Dec-03 13:58
MemberMichael Geddes23-Dec-03 13:58 
There is (rightly) a compiler warning that is generated from this code (I had done the same thing myself).

The problem is that the only reason this works is because by convention, the operator<< returns a reference to the stream passed in. The compiler is rightfully wary of casting the base std::ostream to it's implementation st::ostringstream.

There is no nice way of getting round this, unfortunately. Here's my version, designed for LPCTSTR arguments:
#define S_(string)\
(reinterpret_cast<const  std::basic_ostringstream<TCHAR> &> (std::basic_ostringstream<TCHAR>() \
 << string << std::ends)).str().c_str() 

//.ichael G.
GeneralRe: Word of Caution Pin
JWood5-Sep-06 11:09
MemberJWood5-Sep-06 11:09 
Generalwithout preprocessor... Pin
Goran Mitrovic18-Dec-02 13:18
MemberGoran Mitrovic18-Dec-02 13:18 
GeneralRe: without preprocessor... Pin
Taka Muraoka18-Dec-02 13:31
MemberTaka Muraoka18-Dec-02 13:31 
GeneralRe: without preprocessor... Pin
Goran Mitrovic18-Dec-02 13:59
MemberGoran Mitrovic18-Dec-02 13:59 
GeneralRe: without preprocessor... Pin
Taka Muraoka18-Dec-02 14:00
MemberTaka Muraoka18-Dec-02 14:00 
GeneralRe: without preprocessor... Pin
William E. Kempf19-Dec-02 7:57
MemberWilliam E. Kempf19-Dec-02 7:57 
GeneralRe: without preprocessor... Pin
Nik Colman10-Oct-13 6:51
MemberNik Colman10-Oct-13 6:51 
GeneralSTL beginner Pin
Pit M.18-Dec-02 3:14
MemberPit M.18-Dec-02 3:14 
GeneralRe: STL beginner Pin
Taka Muraoka18-Dec-02 3:20
MemberTaka Muraoka18-Dec-02 3:20 
Generalstringstreams are cool Pin
Ryan_Roberts18-Dec-02 1:35
MemberRyan_Roberts18-Dec-02 1:35 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 1:45
MemberTaka Muraoka18-Dec-02 1:45 
GeneralRe: stringstreams are cool Pin
Ryan_Roberts18-Dec-02 2:08
MemberRyan_Roberts18-Dec-02 2:08 
GeneralRe: stringstreams are cool Pin
Daniel Turini18-Dec-02 3:36
MemberDaniel Turini18-Dec-02 3:36 
GeneralRe: stringstreams are cool Pin
Tim Smith18-Dec-02 13:47
MemberTim Smith18-Dec-02 13:47 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 13:55
MemberTaka Muraoka18-Dec-02 13:55 
GeneralRe: stringstreams are cool Pin
Tim Smith18-Dec-02 15:29
MemberTim Smith18-Dec-02 15:29 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 15:34
MemberTaka Muraoka18-Dec-02 15:34 
GeneralRe: stringstreams are cool Pin
Alex S19-Dec-02 19:20
MemberAlex S19-Dec-02 19:20 
GeneralRe: stringstreams are cool Pin
Daniel Turini18-Dec-02 21:49
MemberDaniel Turini18-Dec-02 21:49 
GeneralRe: stringstreams are cool Pin
William E. Kempf19-Dec-02 8:08
MemberWilliam E. Kempf19-Dec-02 8:08 
GeneralRe: stringstreams are cool Pin
Michael Geddes23-Dec-03 13:42
MemberMichael Geddes23-Dec-03 13:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.