如何通过boost::函数参数传递lambda到被覆盖的方法
How to pass lambda to overridden method with boost::function parameter?
我有一个类的构造函数定义如下:
LambdaJSONVisitor();
LambdaJSONVisitor(boost::function<void (const Value &)> f);
LambdaJSONVisitor(boost::function<void (const Object &)> f);
LambdaJSONVisitor(boost::function<void (const KeyValuePair &)> f);
LambdaJSONVisitor(boost::function<void (const Array &)> f);
我试着构造一个这样的对象:
LambdaJSONVisitor setNodeIDVisitor([&](const JSONAPI::Value &val) -> void
{
...
});
当我尝试编译它时,我得到以下编译器错误:
4>netmodelCNetworkAlarmBuilder.cpp(60): error C2668: 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor' : ambiguous call to overloaded function
4> C:workspaceclientprojectsJSONParserAPI/LambdaJSONVisitor.h(21): could be 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor(boost::function<Signature>)'
4> with
4> [
4> Signature=void (const JSONAPI::Array &)
4> ]
4> C:workspaceclientprojectsJSONParserAPI/LambdaJSONVisitor.h(20): or 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor(boost::function<Signature>)'
4> with
4> [
4> Signature=void (const JSONAPI::KeyValuePair &)
4> ]
4> C:workspaceclientprojectsJSONParserAPI/LambdaJSONVisitor.h(19): or 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor(boost::function<Signature>)'
4> with
4> [
4> Signature=void (const JSONAPI::Object &)
4> ]
4> C:workspaceclientprojectsJSONParserAPI/LambdaJSONVisitor.h(18): or 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor(boost::function<Signature>)'
4> with
4> [
4> Signature=void (const JSONAPI::Value &)
4> ]
4> while trying to match the argument list '(`anonymous-namespace'::<lambda1>)'
是否可以将lambda作为参数传递给这样的重写构造函数?如果是这样,我做错了什么,我应该如何改变代码使其工作?我用的是Visual Studio 2010。
谢谢
只有在有派生类型的情况下才能工作。例如,可以通过创建对象并将其转换为正确的类型来解决这个问题,例如
auto lambda = [&](const C&) {};
Func f(static_cast<std::function<void(const C&)>>(lambda));
或不创建像
这样的对象Func f(static_cast<std::function<void(const C&)>>(
[&](const C&) {});
示例使用一些粘合代码,您可以让编译器找出lambda的参数类型并调用相应的构造函数,而无需显式强制转换。然而,你只需要为你的访问者至少提供一个move构造函数。
第一步:创建一个"模板化"的构造函数。我想,LambdaJSONVisitor不能更改,所以您需要一个辅助函数:
template <class Arg>
LambdaJSONVisitor createLJV_Arg(std::function<void(const Arg&)> f)
{ return LambdaJSONVisitor(f); }
现在可以通过显式地提供模板形参来调用该函数:
LambdaJSONVisitor v = createLJV_Arg<Value>( [](Value const&){} );
// ^-- explicitly state the type
第二步:创建一个模板元编程函数,该函数确定lambda的参数类型,并将其显式传递给第一个函数。示例可以在这里找到:
template <class F>
LambdaJSONVisitor createLJV(F&& f)
{
typedef boost::function_types::parameter_types<decltype(&F::operator())>::type args_t;
typedef boost::mpl::at<args_t, boost::mpl::int_<1>>::type arg1_t;
return createLJV_Arg<arg1_t>(std::forward<F>(f));
}
然后是
LambdaJSONVisitor v = createLJV( [](Value const&){} );
当然,如果可以修改LambdaJSONVisitor
,那么只给它一个模板化的构造函数,并在其中进行lambda的参数类型推导。
相关文章:
- 循环依赖,在继承类的情况下使用覆盖方法
- 确保模拟的 GTest 方法覆盖虚拟方法
- 覆盖作为另一个类的好友的虚拟受保护方法
- 如何使用方法覆盖在输出屏幕上显示堆栈整数值
- 是否有一种有效的方法来搜索队列中的关键字并覆盖其值
- 有没有办法仅在两个模板参数相同时才覆盖匹配的模板方法?
- C ++中的方法覆盖:是编译时还是运行时多态性?
- 错误"pure virtual method called",当此方法已被覆盖时
- 覆盖方法并用shared_ptr调用它们的奇怪行为
- 仅适用于某些模板类型的覆盖方法
- 在某些代码中覆盖方法的方式是什么?
- 为什么没有调用我的覆盖方法
- Poylmorphim,构造函数-C 中使用的覆盖方法
- 覆盖方法返回类型,在C 中使用不完整的派生类
- 从基类执行覆盖方法
- 如何自动化覆盖方法
- 在类模板上覆盖方法,编译器看不到它,只能看到其他重载函数
- 为什么不调用这个 Ruby 覆盖方法
- C++ 覆盖方法不起作用
- 代理dll:方法覆盖/方法转发(COM实现继承)