Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-12-06 03:09:26

Hi Roland,

> As a byproduct of my current work I factored out a small utility
> library that implements a virtual constructor.

> // create the type map
> TYPE_MAP(shape);
> // square with default constructor
> TYPE_REGISTER(shape, square);
> // circle with custom constructor taking a double parameter
> TYPE_REGISTER1(shape, circle, double);
> // circle with default constructor
> TYPE_REGISTER(shape, circle);
> int main(int argc, char* argv[])
> {
> std::string type;
> type = "square";
> shape* ps1 = dynamic_new<shape>(type);
> shape* ps2 = dynamic_new<shape>("circle", 20.0);

What if I write:

     shape* ps2 = dynamic_new<shape>("circle", 20);

I tried to create virtual construct functionality for a future Boost.Plugin
library and it worked like this:

   namespace boost { namespace plugin {
   struct virtual_constructors<Weapon> {
       typedef mpl::list<mpl::list<std::string>,
                         mpl::list<std::string, int>
> type;

Given those declaration, instantiaton of boost::plugin_factory<Weapon> will
have a 'get' method for each listed constructor signature, and usual
overload resolution will work. The disadvantage is that you need to
explicitly instantiate 'virtual_construct', but then you get static
checking. For example, a plugin can't forget to define a required

I also considered this syntax:
   struct virtual_constructors<Weapon> {
       typedef mpl::list<ctor (std::string),
                         ctor (std::string, int)> .....

where 'ctor' is auxillary struct, but maybe it's too smart, haven't decided

What would you say?

- Volodya

Boost list run by bdawes at, gregod at, cpdaniel at, john at