How to use boost::ref with Boost.Parameter library when passing arguments?

260 views Asked by At

I use the Boost.Parameter library for providing named parameters to a constructor.

BOOST_PARAMETER_NAME(windowFunction)

namespace detail
{

struct ClassAImpl
{
    template <class ArgumentPack>
    ClassAImpl(ArgumentPack const& args)
        : mWindowFun(args[_windowFunction])
            , [...]
    {

    }

    boost::function<bool(int, int)> mWindowFun;
    [...]
};
}

struct ClassA : detail::ClassAImpl
{
    BOOST_PARAMETER_CONSTRUCTOR(
            ClassA, (detail::ClassAImpl), tag
          , (optional (windowFunction,*)
            [...]))
};

Usually windowFunction will be copied by the boost::function object, however I want to also be able to pass by reference with boost::ref.

However when I pass a function object with boost::ref the reference_wrapper<T> is removed and the ArgumentPack contains a reference to the T value.

Question: Is there a way to prevent the removal of the reference_wrapper<T> wrapper?

Example:

SomeFunctionObject s;
ClassA a(windowFunction = boost::ref(s));

will have SomeFunctionObject& s passed to mWindowFun in the constructor of ClassAImpl instead of const reference_wrapper<SomeFunctionObject>&. Thus, s will be copied by boost::function which is undesirable.

1

There are 1 answers

0
Hannah S. On

This seems currently not possible as Boost Parameter is explicitly unwrapping reference_wrappers.

This is necessary for allowing to pass positional arguments by reference.