c++ - Implicit type conversion is not working -
class test { public: operator string() { return string{"test!"}; } }; int main() { cout << test{}; } i expecting test object implicit converted string , output, gives me error: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue 'std::basic_ostream<char>&&' this explicit conversion works: cout << string{test{}}; i got working casting const char* : class test { public: operator const char*() { return "test!"; } }; then output: cout << test{}; //yay works. i assuming cout << string implicit conversion string char * , if use casting string, not perform 2 level conversion test string char * . after directly casting const char* , works. (please correct if assumption wrong) to prove assumption right class test { public: operator string() { return string{"test!"}; } }; ostream& operator<< ...