【发布时间】: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<T> 被删除,并且 ArgumentPack 包含对 T 值的引用。
问题:有没有办法防止 reference_wrapper<T> 包装器被删除?
例子:
SomeFunctionObject s;
ClassA a(windowFunction = boost::ref(s));
将在ClassAImpl 的构造函数中将SomeFunctionObject& s 传递给mWindowFun,而不是const reference_wrapper<SomeFunctionObject>&。因此,s 将被 boost::function 复制,这是不可取的。
【问题讨论】:
标签: c++ boost boost-parameter boost-ref