Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded Bool/String Ambiguity

Why is C++ casting the string literal I pass in as a bool rather than a string?

#include <iostream>

using namespace std;

class A
{
    public:
        A(string v)
        {
            cout << v;
        }

        A(bool v)
        {
            cout << v;
        }
};

int main()
{
    A("hello");
    return 0;
}

Output: 1

Is it because the compiler isn't smart enough to make the jump from char * to string and rather just assumes that bool is the closest thing to a pointer? Is my only option to make an explicit char * constructor that basically does the exact same thing as the string constructor?

like image 339
Gillespie Avatar asked Oct 16 '14 21:10

Gillespie


3 Answers

If you have C++11 you can use a delegating constructor:

A(char const* s) : A(std::string(s)) { }

The reason the boolean converting-constructor is chosen over the one for std::string is because the conversion from char const* to bool is a standard conversion while the one to std::string is a user-defined conversion. Standard conversions have a greater rank than user-defined conversions.

like image 70
David G Avatar answered Nov 19 '22 18:11

David G


With

A(string("hello"));

it will give the expected result.

Why is it so ?

It's because of the standard conversions:

  • "hello" is understood as a const char* pointer
  • this pointer can be converted to a bool (section 4.12 of the standard: "A prvalue of (...) pointer (...) type can be converted to a prvalue of type bool." )
  • the conversion from "hello" to string is not considered because section 12.3 of standard explains that "Type conversions of class objects can be specified by constructors and by conversion functions. These conversions are called user-defined conversions" and "User-defined conversions are applied only where they are unambiguous". Wouldn't you have the bool constructor the std::string conversion would be done implicitely.

How to get what you expected ?

Just add the missing constructor for string litterals:

A(const char* v)
{
    cout << v;  // or convert v to string if you want to store it in a string member
}

Of course, instead of rewriting a constructor from scratch, you could opt for a delegate as suggested by 0x499602D2 in another answer.

like image 5
Christophe Avatar answered Nov 19 '22 20:11

Christophe


Recently I passed this problem too, let me share another way.

You can change the bool constructor to a unsigned char. So the decay and implict conversion of string literal don't happen, and the std::string constructor takes place.

class A
{
public:
    A(string v)
    {
        cout << v;
    }

    A(unsigned char v)
    {
        cout << static_cast<bool>(v);
    }
};

int main()
{
     A("Hello"); // <- Call A(string)
     A(false);   // <- Call A(unsigned char)
}

This way you don't need to provide always overloads to std::string and const char* neither making code bloat constructing the std::string at the client call site.

I don't claim that's better, but it's simpler.

like image 3
Nico Engels Avatar answered Nov 19 '22 18:11

Nico Engels