[C++-sig] [Py++] Does pyplusplus respect explicit constructors?

Christopher Bruns cmbruns at stanford.edu
Wed Sep 30 18:18:17 CEST 2009


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< >() );
}

##################################


More information about the Cplusplus-sig mailing list