Boost logo

Boost :

Subject: Re: [boost] typeof or similar
From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2010-02-25 10:22:01


Steffen Roeber wrote:
>
> My preferred way would be something like a factory:
>

...

>
> I think there is no "normal" C++ language support. But Boost has some
> methods that by-pass some language constraints. I took a loot at something
> like 'typeof'. But I don't get the things together.
>

The trunk version of Boost provides a ready-made factory as a
function object.

It can be assigned to a Boost.Function with an appropriate
signature if you need an opaque, polymorphic handle. E.g:

     #include <boost/functional/factory.hpp>
     #include <boost/function.hpp>
     #include <map>

     typedef boost::function< an_abstract_class*() > a_factory;

     // [...]

     int main()
     {
         std::map<std::string,a_factory> factories;

         factories["a_name"] = boost::factory<a_concrete_class*>();
         factories["another_name"] =
             boost::factory<another_concrete_class*>();

         // [...]

         an_abstract_class* something = factories["a_name"]();

         // [...]
     }

Regards,
Tobias


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk