[C++-sig] Virtual methods with arguments and boost::cref

Charles Solar charlessolar at gmail.com
Sun Mar 7 07:23:12 CET 2010

I have some virtual methods with default implementations that I am defining
according the guidelines on the documentation and I ran into some unexpected
If I define a virtual method in boost python like so

void receivedMsg( const sl::Message& msg )
        if( bp::override func_receivedMsg = this->get_override(
"receivedMsg" ) )
            func_receivedMsg( boost::cref( msg ) );

it works fine, my receivedMsg function gets called in python no problem.
Note that I am not calling the default implementation because the default
implementation does not do anything, I do not want to make them pure virtual
because the idea is that the user only has to define the methods he wants
notifications on.

My trouble comes when I do

void receiverDeleted( const std::string& type, const std::string& name )
        if( bp::override func_receiverDeleted = this->get_override(
"receiverDeleted" ) )
            func_receiverDeleted( boost::cref( type ), boost::cref( name )

I get a error_already_set exception because the mechanics under the hood
cannot pythonize the boost::cref( type ).  In my first example, Message is
defined in python so I guess cref works like that, but the default converter
for std::string must not like the boost::cref wrapper.  I do not get the
exception and it seems to work fine if I remove the crefs.

I added the cref's a while ago because it seemed like the right thing to
do.  I do not want boost::python creating copies of my objects under the
hood, maybe it does anyway, I do not know.

Do the crefs do anything?  Should I even have them?  Is this a bug or just
me being stupid? :p

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20100307/442aab06/attachment.html>

More information about the Cplusplus-sig mailing list