Click here to Skip to main content
Click here to Skip to main content

Quick and convenient way to build STL strings.

, 9 Jul 2013
Rate this:
Please Sign up or sign in to vote.
A quick and convenient way to build STL strings.

Introduction

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 Smile | :)

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 ) ;

Conclusion

And that's it! Article count: 2.

License

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

Share

About the Author

Taka Muraoka
Awasu
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 936038010-Jul-13 14:35 
GeneralMemory address being printed Pinmemberan_phu28-Oct-04 8:32 
AnswerRe: Memory address being printed [modified] Pinmemberashkira5-Jul-09 22:27 
GeneralWord of Caution PinmemberMichael Geddes23-Dec-03 12:58 
GeneralRe: Word of Caution PinmemberJWood5-Sep-06 10:09 
Generalwithout preprocessor... PinsussGoran Mitrovic18-Dec-02 12:18 
GeneralRe: without preprocessor... PinmemberTaka Muraoka18-Dec-02 12:31 
GeneralRe: without preprocessor... PinsussGoran Mitrovic18-Dec-02 12:59 
GeneralRe: without preprocessor... PinmemberTaka Muraoka18-Dec-02 13:00 
GeneralRe: without preprocessor... PinmemberWilliam E. Kempf19-Dec-02 6:57 
GeneralRe: without preprocessor... PinmemberNik Colman10-Oct-13 5:51 
GeneralSTL beginner PinmemberPit M.18-Dec-02 2:14 
GeneralRe: STL beginner PinmemberTaka Muraoka18-Dec-02 2:20 
Generalstringstreams are cool PinmemberRyan S Roberts18-Dec-02 0:35 
GeneralRe: stringstreams are cool PinmemberTaka Muraoka18-Dec-02 0:45 
GeneralRe: stringstreams are cool PinmemberRyan S Roberts18-Dec-02 1:08 
GeneralRe: stringstreams are cool PinmemberDaniel Turini18-Dec-02 2:36 
GeneralRe: stringstreams are cool PinmemberTim Smith18-Dec-02 12:47 
GeneralRe: stringstreams are cool PinmemberTaka Muraoka18-Dec-02 12:55 
GeneralRe: stringstreams are cool PinmemberTim Smith18-Dec-02 14:29 
GeneralRe: stringstreams are cool PinmemberTaka Muraoka18-Dec-02 14:34 
GeneralRe: stringstreams are cool PinsussAlex S19-Dec-02 18:20 
GeneralRe: stringstreams are cool PinmemberDaniel Turini18-Dec-02 20:49 
GeneralRe: stringstreams are cool PinmemberWilliam E. Kempf19-Dec-02 7:08 
GeneralRe: stringstreams are cool PinmemberMichael Geddes23-Dec-03 12:42 
GeneralAh, the power of macros PinmemberMarc Clifton18-Dec-02 0:32 
GeneralRe: Ah, the power of macros PinmemberTaka Muraoka18-Dec-02 0:37 
GeneralRe: Ah, the power of macros PinmemberMarc Clifton18-Dec-02 0:56 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 9 Jul 2013
Article Copyright 2002 by Taka Muraoka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid