如何通过boost::函数参数传递lambda到被覆盖的方法

How to pass lambda to overridden method with boost::function parameter?

本文关键字:覆盖 方法 参数传递 何通过 boost 函数 lambda      更新时间:2023-10-16

我有一个类的构造函数定义如下:

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的参数类型推导。