[C++-sig] register c++ std::pair<string, string> and std::pair<string*, string*>

MM finjulhich at gmail.com
Fri Aug 28 16:15:50 CEST 2015


On 28 August 2015 at 15:01, Stefan Seefeld <stefan at seefeld.name> wrote:

> On 28/08/15 09:26 AM, MM wrote:
> > This is in too separate modules.
> > The converter is registered in module1, and the class_<range_t> in
> > module 2.
> > I import 1 then 2, then it happens
> >
> > Also the intents are different. THe pair<string,string> is to be
> > considered as a (str1,str2) tuple of 2 values.
> > The pair<string*, string*> is the [begin, end[ iterators into an array
> > with value_type string
>
>
> Oups, sorry, I hadn't noticed that they had different types. Can you
> please post a minimal bug self-contained test case ? I'd like to debug
> this a bit to better understand what is going on. I believe this should
> work as there is no reason the two types should be confused. (It
> obviously depends on your "Converter" type, which you haven't shown.)
>
> Thanks,
>     Stefan
>
>
Well, there's something about the fact that pointer types somehow change to
the types pointed to.... I don't understand it well

I tried to strip it down here:

/// header file
template <typename T1, typename T2>
struct stdpair_to_python_tuple
{
  static PyObject* convert(const std::pair<T1,T2>&pr)
  {
    return incref( make_tuple(pr.first, pr.second).ptr() );
  }
};

/// module1.cpp
BOOST_PYTHON_MODULE(libmodule1)
{
  to_python_converter<
      std::pair<std::string, std::string>,
      stdpair_to_python_tuple<std::string,std::string>>();
}

/// module2.cpp
typedef std::pair<const std::string*,const std::string*> range_t;
range_t range;

BOOST_PYTHON_MODULE(libmodule2)
{
  using namespace boost::python;

  class_<range_t>("range")
    .def("__iter__",
  range(&range_t::first, &range_t::second))
    ;

  scope().attr("allitems") = object(ptr(&range));
}


Load 1 then 2 it should trigger the warning.
Apologies for missing the includes.

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20150828/fe8ad2b8/attachment.html>


More information about the Cplusplus-sig mailing list