[Py++] Does pyplusplus respect explicit constructors?
Single-argument C++ constructors in pyplusplus seem to generate a boost::python::implicitly_convertible tag, even when the constructor is declared "explicit" in C++. That doesn't sound right to me. Shouldn't a pair of types that are not implicitly convertible in C++, also not be implicitly convertible in python? I suspect I can manually adjust the implicitly_convertible tag generation using the "allow_implicit_conversion" flag in pyplusplus. Is there a way to tell pyplusplus to avoid generating an implicitly_convertible tag for all explicit constructors? ########## test.h input source ############ class Foo { public: Foo() {} }; class Bar { public: // explicit constructor means don't implicitly convert Foo to Bar in C++ explicit Bar(Foo&) {} }; ############################### ########## wrap.py wrapping code #### from pyplusplus import module_builder mb = module_builder.module_builder_t(["test.h"] , gccxml_path = "C:\\Program Files\\gccxml_sherm\\bin\\gccxml.exe") mb.build_code_creator( module_name='test' ) mb.write_module( 'test_explicit.cpp' ) ################################## ##### generated test_explicit.cpp ###### // This file has been generated by Py++. #include "boost/python.hpp" #include "test.h" namespace bp = boost::python; BOOST_PYTHON_MODULE(test){ { //::Bar typedef bp::class_< Bar > Bar_exposer_t; Bar_exposer_t Bar_exposer = Bar_exposer_t( "Bar", bp::init< Foo & >(( bp::arg("arg0") )) ); bp::scope Bar_scope( Bar_exposer ); bp::implicitly_convertible< Foo &, Bar >(); // <-- *** I'm surprised by this *** } bp::class_< Foo >( "Foo", bp::init< >() ); } ##################################
On Wed, Sep 30, 2009 at 6:18 PM, Christopher Bruns <cmbruns@stanford.edu> wrote:
Single-argument C++ constructors in pyplusplus seem to generate a boost::python::implicitly_convertible tag, even when the constructor is declared "explicit" in C++. That doesn't sound right to me.
I don't completely agree with you.
Shouldn't a pair of types that are not implicitly convertible in C++, also not be implicitly convertible in python?
It depends on your use cases.
I suspect I can manually adjust the implicitly_convertible tag generation using the "allow_implicit_conversion" flag in pyplusplus. Is there a way to tell pyplusplus to avoid generating an implicitly_convertible tag for all explicit constructors?
I don't think so. I attached gccxml generated file for your code. As you can see both constructors ( Foo and Bar ) have "explitic=1". So Py++ has no way to find out whether a constructor explicit or not. If such functionality would present, I think that the default would be the opposite. HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/
participants (2)
-
Christopher Bruns -
Roman Yakovenko