I am curious if std::cout has a return value, because when I do this:
cout << cout << "";
some hexa code is printed. What’s the meaning of this printed value?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because the operands of
cout << coutare user-defined types, the expression is effectively a function call. The compiler must find the bestoperator<<that matches the operands, which in this case are both of typestd::ostream.There are many candidate operator overloads from which to choose, but I’ll just describe the one that ends up getting selected, following the usual overload resolution process.
std::ostreamhas a conversion operator that allows conversion tovoid*. This is used to enable testing the state of the stream as a boolean condition (i.e., it allowsif (cout)to work).The right-hand operand expression
coutis implicitly converted tovoid const*using this conversion operator, then theoperator<<overload that takes anostream&and avoid const*is called to write this pointer value.Note that the actual value resulting from the
ostreamtovoid*conversion is unspecified. The specification only mandates that if the stream is in a bad state, a null pointer is returned, otherwise a non-null pointer is returned.The
operator<<overloads for stream insertion do have a return value: they return the stream that was provided as an operand. This is what allows chaining of insertion operations (and for input streams, extraction operations using>>).