Boost logo

Boost Users :

Subject: Re: [Boost-users] Default argument values with boost::function
From: Eric MALENFANT (Eric.Malenfant_at_[hidden])
Date: 2009-04-30 15:10:48


Artyom wrote:
> Is there any way to set default argument values with Boost::Function?
> On VS2008 SP1 I tried:
>
> boost::function< void( unsigned i = 1 ) > increment;
>
> But this didn't work.

I don't think this is possible with boost::function. That reminds me
Boost.Overload, which was discussed some time ago on the developers
mailing list. There's something in the sandbox:
https://svn.boost.org/trac/boost/browser/sandbox/overload/trunk/libs/ove
rload but I'm not aware of its status.

A quick and dirty alternative may be something like:

namespace bft = boost::function_types;
namespace bmpl = boost::mpl;

template<
    typename FuncT,
    typename bmpl::at<bft::parameter_types<int(int)>, bmpl::int_<0>
>::type DefaultArg
>
struct UnaryFuncWithDefault
{
    typedef typename bft::result_type<FuncT>::type ResultType;
    typedef typename bmpl::at<bft::parameter_types<int(int)>,
bmpl::int_<0> >::type ArgType;

    UnaryFuncWithDefault(const boost::function<FuncT>& Func) :
m_Func(Func){}

    ResultType operator()(){return m_Func(DefaultArg);}

    ResultType operator()(ArgType Arg){return m_Func(Arg);}

    boost::function<FuncT> m_Func;
};


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net