operator << for std::string in #define
  Home FAQ Contact Sign in
comp.lang.c++.moderated only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++.moderated Profile…
 Up
operator << for std::string in #define         


Author: probstm
Date: May 7, 2007 23:20

I am using a message handler class that implements amongst others:

static void message ( std::string aHeader,
std::string aMessage,
int aTimeout = 0,
MessageType aFlag =
MSG_Information );

I then use a set of defines, e.g.:

#define USR_MSG( msg ) MSG::message( "", msg, 0, MSG_User )

The problem is now that I get a compiler error when I write

USR_MSG("The value of x is " << x);

Does anybody have a smart solution for that? I thought about rerouting
std::cout but it doesn't seem to be the best way to me.

Thanks a lot for your help,

Martin.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
4 Comments
Re: operator << for std::string in #define         


Author: Carl Barron
Date: May 8, 2007 06:20

In article <1178602787.457440.313660@h2g2000hsg.googlegroups.com>,
gmail.com> wrote:
> I am using a message handler class that implements amongst others:
>
> static void message ( std::string aHeader,
> std::string aMessage,
> ...
Show full article (1.22Kb)
no comments
Re: operator << for std::string in #define         


Author: Roman.Perepelitsa
Date: May 8, 2007 10:00

{ Edits: quoted signature and clc++m banner removed. Tip: most
newsreaders will remove the clc++m banner automatically. -mod }
Show full article (1.41Kb)
no comments
Re: operator << for std::string in #define         


Author: Ulrich Eckhardt
Date: May 8, 2007 10:10

probstm@gmail.com wrote:
> static void message ( std::string aHeader,
> std::string aMessage,
> int aTimeout = 0,
> MessageType aFlag = MSG_Information );
>
> I then use a set of defines, e.g.:
>
> #define USR_MSG( msg ) MSG::message( "", msg, 0, MSG_User )
>
> The problem is now that I get a compiler error when I write
>
> USR_MSG("The value of x is " << x);

You would always get a compile error when you write

"x is " << x

in a context where that is evaluated as is. FYI: in the context of

std::cout << "x is " << x << std::endl;

the expressions are evaluated as

(((std::cout << "x is ") << x) << std::endl);
Show full article (1.73Kb)
no comments
Re: operator << for std::string in #define         


Author: Roman.Perepelitsa
Date: May 11, 2007 01:40

> #define USR_MSG(msg)\
> do\
> {\
> std::ostringstream os;\
> os << msg;\
> USR:message("",os.str(),0,MSG_User);\
>
> }while(0)

It's bad idea to use 'os' as the name of temporary object inside
macro. Consider this example:

std::string os = "Linux";
USR_MSG("Running on " << os); // You are doomed

Here USR:message will be called with second parameter equal to
something like "Running on 0012FEF8". This bug can be really hard one
to track down.

Conclusion: always use loooooong and low-chance-to-occur-in-user-code
names inside macros. Using __LINE__ as a part of the name is also a
good idea.

Roman Perepelitsa.
Show full article (0.78Kb)
no comments