【问题标题】:How to use boost::ref with Boost.Parameter library when passing arguments?传递参数时如何将 boost::ref 与 Boost.Parameter 库一起使用?
【发布时间】:2011-11-10 14:49:08
【问题描述】:

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

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&lt;T&gt; 被删除,并且 ArgumentPack 包含对 T 值的引用。

问题:有没有办法防止 reference_wrapper&lt;T&gt; 包装器被删除?

例子:

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

将在ClassAImpl 的构造函数中将SomeFunctionObject&amp; s 传递给mWindowFun,而不是const reference_wrapper&lt;SomeFunctionObject&gt;&amp;。因此,s 将被 boost::function 复制,这是不可取的。

【问题讨论】:

    标签: c++ boost boost-parameter boost-ref


    【解决方案1】:

    目前这似乎是不可能的,因为 Boost 参数明确解开 reference_wrappers。

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

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 2018-08-24
      • 2019-05-05
      • 1970-01-01
      • 2012-10-14
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      相关资源
      最近更新 更多