gpt4 book ai didi

c++ - 传递参数时如何将 boost::ref 与 Boost.Parameter 库一起使用?

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:10 25 4
gpt4 key购买 nike

我使用 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<T>被删除并且 ArgumentPack 包含对 T 的引用值(value)。

问题:有没有办法防止删除 reference_wrapper<T>包装器?

例子:

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

会有SomeFunctionObject& s传递给 mWindowFunClassAImpl 的构造函数中而不是 const reference_wrapper<SomeFunctionObject>& .因此,s将被 boost::function 复制这是不可取的。

最佳答案

目前这似乎是不可能的,因为 Boost Parameter 正在显式展开 reference_wrapper

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

关于c++ - 传递参数时如何将 boost::ref 与 Boost.Parameter 库一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8081337/

25 4 0