相关疑难解决方法(0)

为什么C++ 11没有隐式地将lambda转换为std :: function对象?

我实现了一个通用事件发射器类,它允许代码注册回调,并使用参数发出事件.我使用Boost.Any类型擦除来存储回调,以便它们可以具有任意参数签名.

这一切都有效,但由于某种原因,传入的lambdas必须首先变成std::function对象.为什么编译器不推断lambda是函数类型?是因为我使用可变参数模板的方式吗?

我使用Clang(版本字符串:) Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn).

码:

#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#include <boost/any.hpp>


using std::cout;
using std::endl;
using std::function;
using std::map;
using std::string;
using std::vector;


class emitter {

   public:

      template <typename... Args>
      void on(string const& event_type, function<void (Args...)> const& f) {
         _listeners[event_type].push_back(f);
      }

      template <typename... Args>
      void emit(string const& event_type, Args... args) {
         auto listeners = _listeners.find(event_type);
         for (auto l : listeners->second) {
            auto lf …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates template-function c++11

15
推荐指数
2
解决办法
2276
查看次数

标签 统计

c++ ×1

c++11 ×1

lambda ×1

template-function ×1

templates ×1