[C++-sig] implicit conversion from python list

James Pelletier jamesp at trdlnk.com
Mon Jul 31 19:51:52 CEST 2006


I'm trying to create an implicit conversion from a native python list to 
a user-defined C++ object. The conversion seems to work fine for an 
integer, but the same conversion doesn't seem to work for a list. Is 
this supported (I'm using boost-1.32)?

James

------------- test.C --------------

#include <boost/python.hpp>

using namespace boost::python;

class X {
public:
	X(int) {}
	X(list) {}
};

void f( X ) {}

BOOST_PYTHON_MODULE(boostpy)
{
	class_<X>( "X", init<int>()[return_value_policy<manage_new_object>()] )
		.def( init<list>()[return_value_policy<manage_new_object>()] );

	implicitly_convertible<int, X>();
	implicitly_convertible<list, X>();

	def( "f", &f );
}

-----------------------------------

 >>> from boostpy import *
 >>> X(32)
<boostpy.X object at 0x81daeb4>
 >>> X([])
<boostpy.X object at 0x81dd374>
 >>> f(32)
 >>> f(X([]))
 >>> f([])
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
     boostpy.f(list)
did not match C++ signature:
     f(X)
 >>>



More information about the Cplusplus-sig mailing list