【发布时间】:2013-03-27 15:53:06
【问题描述】:
我有一个类,其构造函数定义如下:
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>netmodel\CNetworkAlarmBuilder.cpp(60): error C2668: 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor' : ambiguous call to overloaded function
4> C:\workspace\client\projects\JSONParser\API/LambdaJSONVisitor.h(21): could be 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor(boost::function<Signature>)'
4> with
4> [
4> Signature=void (const JSONAPI::Array &)
4> ]
4> C:\workspace\client\projects\JSONParser\API/LambdaJSONVisitor.h(20): or 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor(boost::function<Signature>)'
4> with
4> [
4> Signature=void (const JSONAPI::KeyValuePair &)
4> ]
4> C:\workspace\client\projects\JSONParser\API/LambdaJSONVisitor.h(19): or 'JSONAPI::LambdaJSONVisitor::LambdaJSONVisitor(boost::function<Signature>)'
4> with
4> [
4> Signature=void (const JSONAPI::Object &)
4> ]
4> C:\workspace\client\projects\JSONParser\API/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。
谢谢
【问题讨论】:
-
简单示例运行良好liveworkspace.org/code/9YSdj$5
-
是的,我想知道这是否是 VS2010 特定的问题,只是出于兴趣,我尝试更改构造函数签名以将 const refs 用于函数以匹配您的示例,但我得到了同样的错误。
标签: c++ visual-studio-2010 boost c++11 lambda