The problem you are facing (besides operator std::string() returning bool) is that implicit conversions are triggered when you want and when not.
When the compiler sees s = t , it identifies the following potential std::operator= :
// using std::string for compactness instead of the full template std::string::operator=( std::string const & ); std::string::operator=( char );
Now t is not one of them, so it tries to convert it to something that can fit, and finds two ways: convert to bool, which can be promoted to char or converted to std::string directly. The compiler cannot decide and refuse.
This is one of the reasons you want to avoid providing many different conversion operators. Anything that can be implicitly called by the compiler will eventually be called if you don't think it should be.
This article article addresses this issue. This sentence, instead of converting to bool , provides conversion to a member function
class testable { typedef void (testable::*bool_type)(); void auxiliar_function_for_true_value() {} public: operator bool_type() const { return condition() ? &testable::auxiliar_function_for_true_value : 0; } bool condition() const; };
If an instance of this class is used inside the condition ( if (testable()) ), the compiler will try to convert to bool_type , which can be used in state.
EDIT
After commenting on how this code is more complex with this solution, you can always provide it as a general small utility. After you provide the first part of the code, complexity is encapsulated in the header.
// utility header safe_bool.hpp class safe_bool_t; typedef void (safe_bool_t::*bool_type)(); inline bool_type safe_bool(bool); class safe_bool_t { void auxiliar_function_for_true_value() {} friend bool_type safe_bool(bool); }; inline bool_type safe_bool(bool) { return condition ? &safe_bool_t::auxiliar_function_for_true_value : 0; }
Now your class becomes much simpler, and it is read by itself (by choosing the appropriate names for functions and types):
// each class with conversion class testable { public: operator bool_type() { return safe_bool(true); } };
Only if the reader is interested in knowing how the safe_bool idiom safe_bool implemented, and reading the heading that they fill in, safe_bool encounter complexity (which can be explained in the comments).