L
lallous
Hello
I want to write a function that can be called as:
printmessage("hello world, i=" << i << ", j=" << j);
I managed to do it as this:
#define printmessage(fmt) { static stringstream strm; strm << fmt; cout <<
strm.rdbuf() << endl; }
And call it as:
printmessage("hello world, i=" << i << ", j=" << j);
It is not a function though, but it looks like a function and is easily
called.
Could not make it a function as i would have to first create a stringstream
variable then pass its rdbuf() to my printing function.
Can someone suggest a better way?
If I define 'strm' in the macro as 'static' would that be better performance
wise, as the object will not be created / destroyed whenever i am creating a
new scope in that macro?
Why in this:
#define prints(fmt) { s << fmt; cout << s.rdbuf() << endl; }
stringstream s;
prints("hello");
prints("world!");
Why cout << s.rdbuf() shows: "hello\nworld" and not "hello\nhello\nworld\n"
?
I want to write a function that can be called as:
printmessage("hello world, i=" << i << ", j=" << j);
I managed to do it as this:
#define printmessage(fmt) { static stringstream strm; strm << fmt; cout <<
strm.rdbuf() << endl; }
And call it as:
printmessage("hello world, i=" << i << ", j=" << j);
It is not a function though, but it looks like a function and is easily
called.
Could not make it a function as i would have to first create a stringstream
variable then pass its rdbuf() to my printing function.
Can someone suggest a better way?
If I define 'strm' in the macro as 'static' would that be better performance
wise, as the object will not be created / destroyed whenever i am creating a
new scope in that macro?
Why in this:
#define prints(fmt) { s << fmt; cout << s.rdbuf() << endl; }
stringstream s;
prints("hello");
prints("world!");
Why cout << s.rdbuf() shows: "hello\nworld" and not "hello\nhello\nworld\n"
?