鉴于lambda的类型是可确定的(可强制转换为a std::function(请纠正我,如果我错了,请纠正我)),重载函数应该同时使用两个函子。已定义?([&]() -> Type {})
请注意,对于我当前的解决方案,我需要按引用捕获,这就是为什么代码包含其逻辑的原因。
以下示例描述了该问题:
#include <iostream>
#include <string>    
#include <functional>
void do_some(std::function<void(int)> thing) 
{
   thing(5);
}
void do_some(std::function<bool(int)> thing)
{
   if (thing(10)) 
   {
      std::cout << "it's true!" << std::endl;
   }
}
int main()
{
   int local_to_be_modified = 0;
   do_some(
      [&](int in)
      {
         local_to_be_modified = in;
         std::cout << "This is void-" << std::endl;
      }
   );
   do_some(
      [&](int in) -> bool
      { 
         // error: call to 'do_some' is ambiguous
         local_to_be_modified += in;
         std::cout << "This is bool-" << std::endl;
         return true;
      }
   );
}
std::function<void(int)>甚至可以从返回某些内容的lambda构造出(因为这会导致返回值被忽略)。