jeudi 21 juillet 2016

Implicit conversion for matching overloads C++

I have a problem understanding how function overloading works, it boils down to this simple example:

class Foo {
public:
  void bar(const bool & val) {} 
};

void DoFn(std::function<void( Foo*, const wxString&)> fn ) {}

if I now call DoFn like so:

DoFn( &Foo::bar );

it compiles fine. How is it converting from bool to wxString within the template parameter for std::function? If I change wxString to std::string then it fails to compile as expected.

(wxString is a class in wxWidgets, version 2.8)

What I really don't understand is how the wxString class is able to declare it can be converted to from a bool.

I have tried making a class as follows:

class FakeString {
 public:
  FakeString(bool) {};
  FakeString(const bool &){};
};

and replacing wxString in DoFn with FakeString, but it doesn't compile saying:

could not convert '&Foo::bar' from 'void (Foo::*)(const bool&)' to 'std::function<void(Foo*, const FakeString&)>'

Aucun commentaire:

Enregistrer un commentaire