That operator is a free template
function. User defined conversions do not get checked when matching against a template
function arguments, it instead uses type pattern matching (substitution).
In theory a SFINAE overload using std::is_convertable<>
would be able to do what you want, but that technique was not used when operator<<
that outputs a std::string
to a basic_ostream<char>
was defined.
A manual overload to output your class to basic_ostream<...>
will fix your problem.
I would do this:
struct String {
std::string s;
operator std::string() const {return s;}
friend std::ostream& operator<<( std::ostream& os, String const& self) {
return os<<self.s;
}
};
which has the added benefit of not creating a wasted copy.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…