<pr*****@gmail.comwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...
: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.
Here's a hack-ish macro that uses a stringstream for streaming,
and returns a pointer to a temporary C-string:
template<class C>
inline C& stripConstFromTemporaryRef(C const& r) { return (C&)r; }
#define STRM2CSTR( ss ) \
((::std::ostringstream&) \
(stripConstFromTemporaryRef(::std::ostringstream() ) \
<<ss )).str().c_str()
(NB: the strip... function is needed in case the first streamed
value uses a non-member << function )
Then you would define your macro as:
#define USR_MSG(msg) MSG::message( "", STRM2CSTR(msg), 0, MSG_User )
Of course this adds some run-time overhead in the simple case
where a static string is to be streamed. There should be a way
to use some smart template tricks to optimize-out the simple
case, but I have never bothered looking into this.
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <
http://www.brainbench.com