提升功能模板错误 [英] boost function template error

查看:51
本文介绍了提升功能模板错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个类来管理一些回调,例如鼠标单击或显示(需要刷新窗口时调用的回调)

I'm developping a class for manage some callbacks such mouse click or display (callback called when window need to be refreshed)

回调和句柄保存在地图中.我正在尝试创建一个可以在地图中注册回调的函数(addCallback).

callbacks and handles are saved in maps. I'm trying to create a function that can register a callback in the maps (addCallback).

class CallBackManager {

public:
  enum CallBackType {
    DISPLAY = 1 << 0,
    MOUSE   = 1 << 1
  };

  template<typename T>
  void AddCallback(CallBackType type, int hwnd, T f) {

    if (type & DISPLAY) {
        addDisplayCallback(hwnd, f); // 50 lines of error here
    }

    if (type & MOUSE) {
        addMouseCallback(hwnd, f);   // The same here
    }

  }

private:

  void addDisplayCallback(int hwnd, boost::function<void ()> f) {
    _display_callback[hwnd] = f;
  };

  void addMouseCallback(int hwnd, boost::function<void (int, int)> f) {
    _mouse_callback[hwnd] = f;
  };

  std::map<int, boost::function<void ()>> _display_callback;
  std::map<int, boost::function<void (int, int)>> _mouse_callback;

};

我这样称呼那些功能:

CallBackManager cbm;

cbm.AddCallBack(
  CallBackManager::CallBackType::DISPLAY,
  my_handle,
  boost::bind(&Foo::Display, this)); // boost::function<void ()>

cbm.AddCallback(
  CallBackManager::CallBackType::DISPLAY,
  my_handle,
  boost::bind(&Foo::Mouse, this, _1, _2)): //boost::function<void (int, int)>

最后错误是:

error C2784: 'T &boost::_bi::list0::operator [](const boost::reference_wrapper<T> &) const' : could not deduce template argument C:\local\boost_1_57_0_b1\boost\bind\bind.hpp   392

我不明白此错误

(这是带有编译错误的代码) https://ideone.com/BdW1Ln

(here is the code with the compilation error) https://ideone.com/BdW1Ln

推荐答案

您要在同一静态代码路径中将T类型的函数添加到两个映射中.当然,您可以对其进行过滤,但这就是运行时.这行不通.

You're adding the function of T type to both maps, in the same static code path. Of course, you filter it, but that's runtime. This can't work.

只需使用单独的功能.

它更短,更易读.

在Coliru上直播

Live On Coliru

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <map>

class CallBackManager {

  public:
    enum CallBackType { DISPLAY = 1 << 0, MOUSE = 1 << 1 };
    struct display_callback_t;

    template <typename F>
    void AddDisplayCallback(int hwnd, F f) {
        addDisplayCallback(hwnd, f);
    }

    template <typename F>
    void AddMouseCallback(int hwnd, F f) {
        addMouseCallback(hwnd, f);
    }

    void test() {
        for (auto& f : _display_callback)
            f.second();
        for (auto& f : _mouse_callback)
            f.second(rand(), rand());
    }
  private:
    void addDisplayCallback(int hwnd, boost::function<void()> f) { _display_callback[hwnd] = f; };

    void addMouseCallback(int hwnd, boost::function<void(int, int)> f) { _mouse_callback[hwnd] = f; };

    std::map<int, boost::function<void()>> _display_callback;
    std::map<int, boost::function<void(int, int)>> _mouse_callback;
};

#include <iostream>
struct Foo {

    int my_handle;
    CallBackManager cbm;

    Foo()
    {
        cbm.AddDisplayCallback(my_handle, boost::bind(&Foo::Display, this));
        cbm.AddMouseCallback(my_handle, boost::bind(&Foo::Mouse, this, _1, _2));
    }

    void Mouse(int, int) {
        std::cout << __PRETTY_FUNCTION__ << "\n";
    }
    void Display() {
        std::cout << __PRETTY_FUNCTION__ << "\n";
    }
};

int main() {
    Foo instance;
    instance.cbm.test();
}

打印

void Foo::Display()
void Foo::Mouse(int, int)

这篇关于提升功能模板错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆