[C++-sig] init from free function instead of constructor
John Reid
j.reid at mail.cryst.bbk.ac.uk
Thu Jan 25 12:34:39 CET 2007
One more related question. :)
So now I want to use implicitly_convertible but I can't get this to do
what I want.
Is there any way of doing something equivalent to:
implicitly_convertible< std::string, X >();
in the example below?
There is no constructor for X that takes a std::string, but when
boost.python is matching arguments at run-time couldn't it realise that
it knows how to make a wrapped X from a std::string and therefore can
convert a string to an X? If so I would like to make this feature request.
Thanks,
John.
John Reid wrote:
> OK this seems to work, I haven't got my head around why return by value
> wouldn't:
>
> struct X {
> typedef boost::shared_ptr< X > ptr;
> std::string _s;
> X( const std::string & s = "test" ) : _s( s ) { }
> };
> X::ptr make_x( ) { return X::ptr( new X ); }
> X::ptr make_x_from_string( const std::string & s ) { return X::ptr( new
> X( s ) ); }
> void export_x()
> {
> using namespace boost::python;
> class_< X > x( "X", no_init );
> x.def( "__init__", make_constructor( make_x ) );
> x.def( "__init__", make_constructor( make_x_from_string ) );
> x.def_readonly( "string", &X::_s );
> register_ptr_to_python< X::ptr >();
> }
>
> Thx,
> John.
More information about the Cplusplus-sig
mailing list