So I got stuck on a simple print-function today and I really don't know how to fix this problem. Basically I want to pass my Strings to a function in a std::cout-style like this:
foo(std::ostringstream() << "Hello" << " World!"); And from what I've read it should be possible by a function along the lines of
void foo(std::ostringstream& in) but when implementing I get a somewhat strange behavior:
#include <iostream> #include <sstream> void foo(std::ostringstream& in) { std::cout << in.str(); } int main() { std::ostringstream working; working << "Hello" << " World!"; foo(working); // OK std::ostringstream notWorking; notWorking << "Hello"; foo(notWorking<<" World!"); // Not OK?! return 0; } While the first call of foo seems fine and works like expected, the second one refuses to compile even though they should (from my perspective) be technically the same thing.
Error:
error C2664: 'void foo(std::ostringstream &)': cannot convert parameter 1 from 'std::basic_ostream<char,std::char_traits<char>>' to 'std::ostringstream &' I'm using MS Visual Studio 2013 Express on Win7 x64