The bool () operator is converted to std :: string and conflicts with the std :: string () operator

How can the bool () operator cause an error when declaring the std :: string operator in a class, as well as how to implicitly convert to a string by itself?

#include <iostream> #include <string> using namespace std; class Test { public: operator std::string() { cout << "op string" << endl; return "whatever";} operator bool() { cout << "op bool" << endl; return true;} }; int main(int argc, char *argv[]) { string s; Test t; s = t; } 
+4
source share
3 answers

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).

+8
source

Your std :: string () statement should return a string, not a bool.

+4
source

As David Rodriguez rightly points out, bool can be upgraded to char and you will get ambiguous overload.

In stl, so that the testable class is usually executed by converting to void * , for example. when you do

 while (istream.getline()) { } 

The loop condition allows false because istream returns NULL in it the void * operator .

Some people argue that this is not as good as in theory, one could do

 void* streamptr = istream; delete streamptr; 

But, in my opinion, if someone starts deleting pointers like this ... he should not be allowed anywhere near the stl code (or C ++, for that matter).

+1
source

Source: https://habr.com/ru/post/1301796/


All Articles