This is a C ++ restriction inherited from older C ++ and ultimately from C.
Your string literals convert to bool better than to the "custom" type std::string .
Instead of casting in the call, you can add a constructor that accepts const char* , perhaps delegating your existing constructor that accepts std::string :
explicit Foo(const char* _id, bool _isVisible = false) : Foo(std::string(_id), _isVisible) {};
This constructor will be an “exact match” (or quite close at all) and will not allow bool to steal the spotlight.
Or, since C ++ 14 uses the literal std::string instead of cast, for example. "hi"s , although you should keep this in mind in callsite, which is "meh" for a general solution.
By the way, if , you really want to take your parameter std::string _id by value, do not make it const ; you stop him from moving and requesting a copy in the member initializer. Depending on what happens on your regular call site, the following may make more sense:
explicit Foo( string _id, bool _isVisible = false ) : id(std::move(_id)) , isVisible(_isVisible) {}
If you usually pass in lvalue, although at least accept const std::string& to prevent one copy.
Lightness races in orbit
source share