Boost logo

Boost :

From: Stjepan Rajko (stipe_at_[hidden])
Date: 2008-08-13 09:58:49


I just realized that named parameters can be implemented rather nicely
using fusion maps (I'm not sure whether this has been explored
before). I threw together an implementation for two particular
parameters/names, but the method could be easily generalized. Here
are examples of functions taking parameter packs (label is a string,
and size is a guigl::size_type which is a gil::point2 type) (...and
don't worry about what guigl is):

using namespace guigl::parameter;

const std::string &function_taking_label(const pack<field::label>::type &args)
    return args.label();

const guigl::size_type &function_taking_size(const
pack<field::size>::type &args)
    return args.size();

const guigl::size_type &function_taking_both(const pack<field::label,
field::size>::type &args)
    return args.size();

Now you can do things like:

const guigl::size_type size_one(1,1);

// parameters can be passed in any order
// size has a default value

// extraneous parameters will be ignored

// empty parameter pack can be used for no parameters (all default values)

Here is the full test code:

and the relevant implementation:

Compared to Boost.Parameter, I have a hunch that the Fusion approach
would be faster to compile (haven't tested that though), and is a
little bit less convoluted (esp. for certain cases like constructors
using named parameters). I've never used Boost.Parameter so I'm not
sure what other comparisons might apply.

Any feedback welcome.

Kind regards,


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