C ++ lambda不会推论函数重载

问题描述

我正在定义一个这样的类:

class foo {
public:
  // I define const and non-const versions of the 'visit' function
  // nb. the lambda is passed by reference
  virtual void visitWith(std::function<void(foo&)>&);
  virtual void visitWith(std::function<void(const foo&)>&) const;
};

foo可以有孩子,所以想法是递归访问foo及其所有孩子。

当我尝试使用它时,例如像这样:

foo f;
f.visitWith([&](const foo&) {
  // Do something here
});

我遇到编译器错误。编译器不知道该怎么办。

我可以通过添加这样的类型转换使其工作:

foo f;
f.visitWith( (std::function<void(const foo&)>) [&](const foo&) {
  // Do something here
});

但这太可怕了。

如何使它整洁地工作?

编辑:

这可能是Visual C ++的问题,它拒绝编译此处给出的代码:

https://ideone.com/n9bySW

我尝试编译时的VC ++输出是:

I get this error in Visual C++

Edit2:不,Visual C ++是正确的,代码不明确。请在下面查看我的解决方案...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)