# Passing a stringstream by reference

This topic is 5118 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm browsing some code here and i discovered the following function: void Log(const std::stringstream &Stream) So i thought: Hey, that's great! That could be a nice way to get rid of my log function, which looks like this: void Log(const std::string &Text, ...); So i assumed i could call the function like this: int Age = 36; Log("Hi, my age is " << Age << "\n"); Appearantly this didn't work. So i don't understand why he made it if you can only use it like this: std::stringstream Stream; int Age = 36; Stream << "Hello, my age is " << Age << "\n"; Log(Stream); Or am I missing something here? I think it could be really useful.

##### Share on other sites
Where did you see that code? From what I can see, passing a stringstream by const reference is a bit pointless, since theres not an awful lot you can do to it except extract the string buffer...

##### Share on other sites
Argh, i just threw that project away...
It was a free engine... i downloaded a few days ago, can't remember where :-S

Anyway, that function seems rather pointless to me.
I tryed to recreate it:

void Log(std::stringstream &Text){	std::string a = Text.str();	MessageBox(GetDesktopWindow(), a.c_str(), "Dumb Function", MB_OK);}

Now, when i call it like this:
Log("Hello, my age is " << 36 << "\nA Test!");

The compiler gives me the following error:
error C2296: '<<' : illegal, left operand has type 'const char [18]'

I'm really baffled here...
What whas on that guy's mind? Or is it just me?

##### Share on other sites
Quote:
 Original post by NixsosNow, when i call it like this:Log("Hello, my age is " << 36 << "\nA Test!");

The code probably used something like

Log("Hello, my age is ") << 36 << "\nA Test!";

or

Log(std::stringstream("Hello, my age is ") << 36 << "\nA Test!");

Quote:
 The compiler gives me the following error:error C2296: '<<' : illegal, left operand has type 'const char [18]'

"Hello, my age is " << 36 isn't legal, and a function's parameters are evaluated before the function is called, so...

##### Share on other sites
Quote:
 Original post by NixsosNow, when i call it like this:Log("Hello, my age is " << 36 << "\nA Test!");The compiler gives me the following error:error C2296: '<<' : illegal, left operand has type 'const char [18]'
Yeah, thats because the compiler tries to call operator<< with "Hello, my age is " and 36 - I.e. call const char*::operator<<(int n). But of course const char* doesn't have an operator<< overloaded. If std::stringstream has a constructor that allows you to construct from a const char*, you might be able to get away with this:
Log(std::stringstream("Hello, my age is ") << 36 << "\nA Test!");
But I haven't used stringstream much, and I'm too lazy to test :P

Quote:
 Original post by NixsosI'm really baffled here...What whas on that guy's mind? Or is it just me?
Nope, its not just you. Assuming theres no way to do this without creating the stream, filling it, then passing it to Log(), he perhaps thought that there'd be more you could do with a stringstream than a plain string. Or maybe he was high :P

EDIT: Beaten by Fruny

##### Share on other sites
Dunno if that is of interest, but you can get the desired effect by using a macro. I know everyone says don't use macros, but I really like doing the following to wrap my logging functions:

#define LOG(_msgStream) std::cout << _msgStream << std::endl

You can use this like:

LOG("Hello " << 123 << "blah");

Another advantage is that when you replace the macro with "#define LOG(_msgStream)", the preprocessor will cut your loglines out of the code just as if they hadn't been there. IMO that's a nice way to turn logging on and off, on a compiler level.

Just my 2 cent.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 11
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632924
• Total Posts
3009232
• ### Who's Online (See full list)

There are no registered users currently online

×