I recently came across a class like the following
class Foo {
public:
Foo(std::string msg) {}
private:
Foo(bool b) {}
};
I noticed that trying to create an object of this class by means of
Foo foo("blah");
causes a compilation error telling that Foo::Foo(bool)
is private. Apparently, if the argument is not an actual std::string, the compiler prefers to use the constructor with the bool
argument. On the other hand, if the private constructor is not given, above code compiles just fine.
Why is it that the "bool
-constructor" has precedence over the "string
-constructor" althogh the type of the passed argument does not fit to any of them? Is this just a matter of definition or does it have a deeper meaning and some good reason?