Friday, 9 August 2013

Explicit type conversion is not working

Explicit type conversion is not working

class Test {
public:
explicit operator string() {
return string{"TEST!"};
}
};
int main() {
cout << Test{};
}
I was expecting the Test object will be explicitly converted to a string
and output, but it gives me error:
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to
'std::basic_ostream<char>&&'
This implicit conversion works:
cout << string{Test{}};

No comments:

Post a Comment