title: Boost Python Expose overloaded member functions returning pointer to class object
body: <p>I have this setup and want to port it to python :</p>
  
  <pre><code>class A {
      public:
          A() {}
          A(string name): name_(name) {}
          ~A() {
              for (auto it = mymap_.begin(); it != mymap.end(); ++it) {
                  delete it-&gt;second;
              }
          }
          string name() const { 
              return name_; 
          }
          bool has_a(string name) const {
              return (mymap_.find(name) != mymap_.end());
          }
          A* a(string name) {
              if (has_a(name)) {
                  return mymap_.at(name);
              }
              return nullptr;
          }
          const A* a(string name) const {
              if (has_a(name)) {
                  return mymap_.at(name);
              }
              return nullptr;
          }
          //Owns pointer
          bool AddA(A* a) {
              if (a &amp;&amp; !has_a(a-&gt;name())) {
                  mymap_.insert(make_pair(a-&gt;name(), a));
                  return true;
              }
              return false;
          }
      private:
          string name_;
          map&lt;string, A*. mymap_;
  };
  </code></pre>
  
  <p>I want to expose overloaded function a() for both const and non const pointers whose ownership should not be passed to python.</p>
  
  <pre><code>void add_a(A&amp; a, std::unique_ptr&lt;A&gt;&amp; ap) {
      a.AddA(ap.get()); ap.release();
  }
  A* (A::*mutable_a)(string) = &amp;A::a;
  const A* (A::*immutable_a)(string) const = &amp;A::a;
  BOOST_PYTHON_MODULE(MyModule) {
     class_&lt;A, boost::nocopyable, boost::shared_ptr&lt;A&gt; &gt;("A", init&lt;string&gt;())
         .def(init&lt;&gt;())
         .def("name", &amp;A::name)
         .def("has_a", &amp;A::has_a)
         .def("a", mutable_a, return_value_policy&lt;copy_non_const_reference&gt;())
         .def("const_a", immutable_a, return_value_policy&lt;copy_const_reference&gt;())
         .def("add_a", add_a, with_custodian_and_ward&lt;1,2&gt;())
     ;
  }
  </code></pre>
  
  <p>It seems to work if return value policy for <code>"a()"</code> and <code>"const_a()"</code> is <code>return_value_policy&lt;reference_existing_object</code>, <code>with_custodian_and_ward_postcall&lt;1,0&gt; &gt;()</code> but not in current setup. Problem with this being that object extracted using <code>const_a()</code> is also mutable in python and it is undesirable.</p>
  
  <p>Error I receive on compilation is :</p>
  
  <blockquote>
    <p>error: no match for call to _(const
    boost::python::detail::copy_non_const_reference_expects_a_non_const_reference_return_type</p>
  </blockquote>
  
  <p>Is there a way to expose this function in python correctly ? I cannot make changes to the base class but I can add some small wrappers like add_a() if that helps me achieve end goal.</p>