I used to display an string by std::cout << str << std::endl, and thought an object implicitly convertabel to std::string can be also displayed in this way.
However, I notice that I was wrong. I cannot std::cout an implicitly converted std::string until I overload operator << for std::string.
The following code demonstrates the mentioned stuff.
#include <stdio.h>
#include <iostream>
#include <string>
class X
{
public:
operator std::string() const {
return std::string("world");
}
};
#ifdef OVERLOAD_STREAM_OP
std::ostream& operator<<(std::ostream& os, const std::string& s) {
return os << s;
}
#endif
int main() {
std::string s = "hello";
std::cout << s << std::endl; // viable
printf("---\n");
X x;
std::cout << x << std::endl; // not viable
return 0;
}
It seems that, in STL implementation, the overloaded operator << function for std::string type, is a little bit different (But I really really don't understand those template stuffs):
template<typename _CharT, typename _Traits, typename _Allocator>
std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const basic_string<_CharT, _Traits, _Allocator>& __str)
{ return __os << __str._M_base(); }
My questions:
What is the difference between STL's overloaded
operator <<and my own overloadedoperator<<forstd::stringtype?Why I cannot display the implicitly converted std::string object
xwithstd::cout(compile error)?
OVERLOAD_STREAM_OPbit, how do you think that's legal? You're adding an overload but none of the types are yours.