c++ - Why can I not use the "<<" operator in a return statement? -


for instance:

std::stringstream formatmemusage(...) {     std::stringstream ss ...     ...     return ss << "mb";   // error here } 

results in the error no suitable user-defined conversion "std::basic_ostream<char, std::char_traits<char>>" "std::stringstream" exists.

i can separate return statement 2 statements ss << "mb"; return ss; , error disappears - why?

(using msvc++ 2010)

because returning last evaluated statement. consider doing:

return ss << "mb"; 

is equivalent to:

return operator<<(ss, "mb"); 

the return type of operator<< in case is, you've seen, std::ostream& - not std::stringstream wanted.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -