[C++-sig] implicitly_convertible or order of init's problem

Roman Yakovenko roman.yakovenko at gmail.com
Mon Sep 18 21:28:49 CEST 2006


Hi. I found some very very strange behaviour.
Please, consider next code:

struct vector{

    vector(){}

    vector( double ){}

    vector( const vector& ){}

};



struct float_vector{

    float_vector(){}

    float_vector( const float_vector& ){}

    float_vector( const vector& ){}

    float_vector( float ){}

};


namespace bp = boost::python;



BOOST_PYTHON_MODULE( ic_ext ){



    bp::class_< float_vector >( "float_vector" )

        .def( bp::init< >() )

        .def( bp::init< vector const & >() )

        .def( bp::init< float >() )

        .def( bp::init< const float_vector& >() );



    bp::implicitly_convertible< vector const &, float_vector >();



    bp::implicitly_convertible< float, float_vector >();



    bp::class_< vector >( "vector" )

        .def( bp::init< >() )

        .def( bp::init< double >() );



    bp::implicitly_convertible< double, vector >();



}


import ic_ext
ic_ext.float_vector( 5.0 )

The last line fails with the error:
TypeError: No registered converter was able to extract a C++ reference to type
vector from this Python object of type float

Question:
Why Boost.Python does not use float_vector( float ) constructor?

If I reorder the init methods "ic_ext.float_vector( 5.0 )" expression pass:

    bp::class_< float_vector >( "float_vector" )
        .def( bp::init< >() )
        .def( bp::init< vector const & >() )
        .def( bp::init< const float_vector& >() )
        .def( bp::init< float >() );

If I comment out "bp::implicitly_convertible< vector const &, float_vector >();"
than "ic_ext.float_vector( 5.0 )" expression pass.

It could be nice to understand why this happens. I attached small example
that reproduce the problem.( I hope this time I did it right :-) )

Thanks.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ic.cpp
Type: text/x-c++src
Size: 864 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060918/f91507a7/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: boost-build.jam
Type: application/octet-stream
Size: 283 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060918/f91507a7/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Jamroot
Type: application/octet-stream
Size: 378 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060918/f91507a7/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: text/x-python
Size: 44 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060918/f91507a7/attachment.py>


More information about the Cplusplus-sig mailing list