title: Wrap python argument for pybind11 overloaded methods
body: <p>I'm trying to create python bindings for some legacy C++ code which implemented its own 'extended' string class <code>aString</code> which it uses extensively. This works fine for <code>.def(py::init&lt;const std::string &amp;, int&gt;())</code>, but once I start trying to wrap overloaded methods pybind11 does not appear to automatically cast <code>std::string</code> to <code>aString</code>. MWE adapted from the pybind11 docs:</p>
  <p><code>src/example.cpp</code>:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;pybind11/pybind11.h&gt;
  
  // Extended string class and struct in legacy library
  class aString : public std::string
  {
  public:
      aString() : std::string() {}
      aString(const char *str) : std::string(str) {}
      aString(const std::string &amp;str) : std::string(str) {}
  };
  
  struct Pet
  {
      Pet(const aString &amp;name, int age) : name(name), age(age) {}
  
      void set(int age_) { age = age_; }
      void set(const aString &amp;name_) { name = name_; }
  
      aString name;
      int age;
  };
  
  // Python bindings
  namespace py = pybind11;
  
  PYBIND11_MODULE(example, m)
  {
      py::class_&lt;aString&gt;(m, &quot;aString&quot;)
          .def(py::init&lt;const std::string &amp;&gt;())
          .def(&quot;__repr__&quot;,
               [](const aString &amp;a)
               {
                   return &quot;&lt;aString ('&quot; + a + &quot;')&gt;&quot;;
               });
  
      py::class_&lt;Pet&gt;(m, &quot;Pet&quot;)
          .def(py::init&lt;const std::string &amp;, int&gt;())
          .def(&quot;set&quot;, static_cast&lt;void (Pet::*)(int)&gt;(&amp;Pet::set), &quot;Set the pet's age&quot;)
          .def(&quot;set&quot;, static_cast&lt;void (Pet::*)(const aString &amp;)&gt;(&amp;Pet::set), &quot;Set the pet's name&quot;)
          .def(&quot;__repr__&quot;,
               [](const Pet &amp;a)
               {
                   return &quot;&lt;Pet ('&quot; + a.name + &quot;')&gt;&quot;;
               });
      ;
  }
  </code></pre>
  <p><code>CMakeLists.txt</code>:</p>
  <pre><code>cmake_minimum_required(VERSION 3.4)
  project(example)
  add_subdirectory(pybind11)
  pybind11_add_module(example src/example.cpp)
  </code></pre>
  <p>In python, the following works fine:</p>
  <pre class="lang-py prettyprint-override"><code>import example
  p = example.Pet(&quot;Penny&quot;, 1)
  p.set(3)
  a = example.aString(&quot;Anne&quot;)
  p.set(a)
  </code></pre>
  <p>However, the following fails with the message:</p>
  <pre><code>#&gt; p.set(&quot;Jenny&quot;)
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-9-c5b0ff7c5315&gt; in &lt;module&gt;
  ----&gt; 1 p.set(&quot;Jenny&quot;)
  
  TypeError: set(): incompatible function arguments. The following argument types are supported:
      1. (self: example.Pet, arg0: int) -&gt; None
      2. (self: example.Pet, arg0: example.aString) -&gt; None
  
  Invoked with: &lt;Pet ('Anne')&gt;, 'Jenny'
  </code></pre>
  <p>I've tried changing my pybinding to be <code>.def(&quot;set&quot;, static_cast&lt;void (Pet::*)(const std::string &amp;)&gt;(&amp;Pet::set), &quot;Set the pet's name&quot;)</code> instead of <code>aString</code>, but this will not compile as <code>std::string</code> is not one of the overload instances. I cannot add an additional overload instance for <code>std:string</code> in the C++ library.</p>
  <p>I would like to be able to call <code>p.set(&quot;Jenny&quot;)</code> and let pybind11 convert <code>&quot;Jenny&quot;</code> into <code>aString(&quot;Jenny&quot;)</code>, rather than <code>p.set(example.aString(&quot;Jenny&quot;))</code>. This is my first project with pybind11 so any help would be appreciated, thanks!</p>