如何在boost中使用boost::ref.传递参数时的形参库

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

本文关键字:boost 参数 形参 ref      更新时间:2023-10-16

我使用Boost。为构造函数提供命名参数的参数库。

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,*)
            [...]))
};

通常windowFunction将被boost::function对象复制,但是我也希望能够通过引用传递boost::ref

然而,当我传递boost::ref函数对象时,reference_wrapper<T>被删除,ArgumentPack包含对T值的引用。

问题:是否有办法防止reference_wrapper<T>包装器的移除?

的例子:

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

将把SomeFunctionObject& s传递给ClassAImpl的构造函数中的mWindowFun,而不是const reference_wrapper<SomeFunctionObject>&。因此,s将被boost::function复制,这是不希望的。

这似乎目前不可能,因为Boost参数显式地展开reference_wrapper s。

这是允许通过引用传递位置参数所必需的。