-1

I've found some unexpected behavior using the VC++2010 compiler with function overloading:

struct A {
  A();
  void O(const bool in);       //(1)
  void O(const std::string in);//(2)
}

Some of the following calls do not resolve like I would think:

A a;
a.O(true);//calls (1)
a.O("what?");//calls (1)
a.O(std::string("better..."));//calls (2)

Can someone explain to me why the second call resolves to the boolean function and what the motivation behind resolving in that way is?

zdp
  • 119
  • 4
  • This is really close since the exact same thing is happening: http://stackoverflow.com/questions/24411134/empty-string-is-interpreted-as-bool-in-constructor – chris Jun 26 '14 at 20:40

1 Answers1

2

The type of "what?" is char const[6], which after decaying to char const* while being passed into the function, is implicitly convertible to bool. The standard conversion takes precedence over user-defined implicit conversions, like the case of converting char const* to std::string.

bstamour
  • 7,746
  • 1
  • 26
  • 39