title: How to wrap a C++ class with a constructor that takes a std::map or std::vector argument with Boost.Python?
body: <p><strong>Disclaimer</strong>: yes, I <em>do know</em> about <code>boost::python::map_indexing_suite</code>. </p>
  
  <p><strong>Task</strong>: I have a C++ class which I want to wrap with Boost.Python. Its constructor takes a <code>std::map</code> argument. Here is the C++ header:</p>
  
  <pre><code>// myclass.hh
  typedef std::map&lt;int, float&gt; mymap_t;
  
  class MyClass {
    public:
    explicit MyClass(const mymap_t&amp; m);
    // ...
  };
  // ...
  </code></pre>
  
  <p>Here is the Boost.Python wrapper (essential parts only):</p>
  
  <pre><code>// myclasswrapper.cc
  #include "mymap.hh"
  #include "boost/python.hpp"
  #include "boost/python/suite/indexing/map_indexing_suite.hpp"
  
  namespace bpy = boost::python;
  
  // wrapping mymap_t
  bpy::class_&lt;mymap_t&gt;("MyMap")
       .def(bpy::map_indexing_suite&lt;mymap_t&gt;())
      ;
  
  // wrapping MyClass
  bpy::class_&lt;MyClass&gt;("MyClass", "My example class",
    bpy::init&lt;mymap_t&gt;()   // ??? what to put here?
  )
    // .def(...method wrappers...)
  ;
  </code></pre>
  
  <p>This compiles. However, I cannot create the mapped <code>MyClass</code> object from the Python side because I don't know what to pass as argument to the constructor. Dictionaries do not get converted to <code>std::map</code>-s automatically:</p>
  
  <pre><code># python test
  myclass = MyClass({1:3.14, 5:42.03})
  </code></pre>
  
  <p>the interpreter complains (rightly so):</p>
  
  <pre><code>Boost.Python.ArgumentError: Python argument types in
      MyClass.__init__(MyClass, dict)
  did not match C++ signature:
      __init__(_object*, std::__1::map&lt;int, float, ...
  </code></pre>
  
  <p>and <code>MyMap</code> on the Python side cannot be initialised with a dictionary either.</p>
  
  <p>Having googled away the best part of a day, I could find only examples for "normal" methods taking <code>std::map</code> arguments that are mapped with <code>.def(...)</code>. And in <code>.def(...)</code> you do not have to specify explicitly the arguments of the mapped method, they are magically discovered. With constructors you must use <code>boost::python::init&lt;...&gt;()</code>, or at least that is what I understood from the docs. </p>
  
  <p><strong>Questions</strong>:</p>
  
  <ol>
  <li>Shall I add something to the <code>MyMap</code> wrapper to help <code>map_indexing_suite</code> convert from a Python dictionary? </li>
  <li>Shall I use a different template argument in <code>boost::python::init&lt;...&gt;</code> in the <code>MyClass</code> wrapper? </li>
  <li>Any other ideas...?</li>
  </ol>
  
  <p><strong>Note</strong>: I have also seen <a href="https://stackoverflow.com/questions/6116345/boostpython-possible-to-automatically-convert-from-dict-stdmap/6118691#6118691">this accepted answer at SO</a>, then I scrolled down and read the comment by @YvesgereY: </p>
  
  <blockquote>
    <p>"For the record, map_indexing_suite solution doesn't work, since no
    implicit "dict->std::map" from_python converter will be applied."</p>
  </blockquote>
  
  <p>And I lost faith :-)</p>