Boost logo

Boost :

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


Hello,

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:
function_taking_label(label("hello"));

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

// parameters can be passed in any order
function_taking_both(label("hello").size(size_one));
function_taking_both(size(size_one).label("hello"));
// size has a default value
function_taking_both(label("hello"));

// extraneous parameters will be ignored
function_taking_label(label("hello").size(size_one));

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

Here is the full test code:

http://pastebin.com/f6b128a4b

and the relevant implementation:

http://pastebin.com/f584c7967

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,

Stjepan


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