[C++-sig] make_constructor strangeness

Jonathan Brandmeyer jbrandmeyer at earthlink.net
Thu Nov 18 04:05:38 CET 2004


On Wed, 2004-11-17 at 21:53 -0500, Neal Becker wrote:
> I noticed that the following didn't work:
> 
>   pyclass
>     .def(init<size_type>())
>     .def("__init__", make_constructor(vector_from_object<value_type>))
> 
> where size_type is some size (probably size_t), value_type could be double
> or std::complex<double>, and vector_from_object has sig:
> template<typename T>
> inline vector<T>* vector_from_object (object& newobject) {
>  
> It seems if I try to construct an object using an argument of type int, the
> init<size_type> is being ignored (silently), and the call goes to
> vector_from_object.
> 
> I guess you can't dispatch an overload to a function that takes "object". 
> Problem is, it seems to act unpredictably, and fails silently.
> 
> Unpredictably, because I used the same code for the case where value_type
> was double, and also when it was std::complex<double>, and the two acted
> differently.  The complex called with an int did call the first
> constructor, while the double one called the second.  AFAICT, there were no
> other significant differences between the two cases.

I believe that overload resolution is performed in reverse order of the
execution of class_<>::def() (same goes for free functions).  Since the
function passed with make_constructor takes a boost::python::object,
that function will _always_ win because any invocation from Python with
only one argument will match, and that signature was added last.  If you
reverse the order such that function taking a boost::python::object gets
wrapped with def first, I think it will work.

HTH,
-Jonathan




More information about the Cplusplus-sig mailing list