[C++-sig] Duplicate class and method definition

André Prins a.h.prins at gmail.com
Mon Feb 22 15:45:07 CET 2010


Hi All,

Recently, I had some problems with a duplicate method and class
definition begin accepted by boost::python (version 1.41 on MSVC).
Consider the following c++-code, which declares a class Foo twice, but
with different bindings for the same function.

/* C++ code: */
#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;

struct Foo
{
    void f( int m ){ std::cerr << "F set to " << m  << std::endl;  }
    void g( int m ){ std::cerr << "G set to " << 10*m << std::endl; }
};

BOOST_PYTHON_MODULE( FooPython )
{
    // First definition binding 'g'
    class_< Foo, boost::noncopyable >( "Foo" )
        .def( "func", &Foo::f )
        ;
    // Second definition binding 'f'
    class_< Foo, boost::noncopyable >( "Foo" )
        .def( "func", &Foo::g )
        ;
}

# Python-code
import FooPython
f = FooPython.Foo()
f.func( 3 )

This outputs the line "G set to 30" and the original function-binding
(to f) is gone. When I remove the boost::noncopyable when creating the
python-binding Foo, I get an assert upon importing FooPython:
    file: libs\python\src\converter\registry.cpp @ line 212
    expression: slot->m_to_python == 0

It is obvious that this is not the intended use of Boost-python, but I
am wondering: why does it assert at runtime without the noncopyable
and why does it "more or less work" with the boost::noncopyable. And
is there a method to detect such multiple definitions of the same
class.

Kind Regards,
André


More information about the Cplusplus-sig mailing list