Boost logo

Boost-Build :

Subject: [Boost-build] Exposing inherited static member variables of the same name. Subtle bug.
From: Luke Titley (luke.titley_at_[hidden])
Date: 2009-01-26 06:10:12


I have defined two boost python classes, one inheriting off the other; each
with a static member variable of the same name.
Compiled with visual studio 2005 this triggers what I believe to be a subtle
bug.

The following is an example of how you might trigger it. I've not compiled
this example code so there might be a few typos.

//---------------------------------------------------------------------------------------
// C++ Classes
//---------------------------------------------------------------------------------------
class Base
{
public:

static const wstring Name;

};
const wstring Base::Name(L"Base");

//---------------------------------------------------------------------------------------
class Derived : public Base
{
public:

static const wstring Name;

};
const wstring Derived ::Name(L"Derived");

//---------------------------------------------------------------------------------------
// Boost Python Bindings
//---------------------------------------------------------------------------------------
wstring getBaseName()
{

return Base::Name;

}
void exposeBase()
{

py::class_<Base>("Base")
  .add_static_property("Name", &getBaseName)
;

}

//---------------------------------------------------------------------------------------
wstring getDerivedName()
{

return Derived::Name;

}
void exposeDerived()
{

py::class_<Derived, bases<Base> >("Derived")
  .add_static_property("Name", &getDerivedName)
;

}

//---------------------------------------------------------------------------------------
BOOST_PYTHON_MODULE(nmx)
{

exposeBase();
exposeDerived();

}

The affect is for the module to fail midway through loading. An exception is
thrown by boost python which is then caught later on by boost python. Any
classes exposed after exposeDerived() will not appear in the module.

My workaround for this is to not name the python properties the same name.
I rename "Name" for Base::Name to "Base_Name" and "Name" for Derived::Name
to "Derived_Name", this seems to fix it. However it might be useful to throw
up a more informative message, or to right something to stderr, it was quite
difficult to find.

Luke



Boost-Build list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk