使用boost ::绑定的boost ::功能:获取绑定变量类型 [英] Using boost::bind with boost::function: retrieve binded variable type

查看:147
本文介绍了使用boost ::绑定的boost ::功能:获取绑定变量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法来检索对的boost ::绑定是什么界或PARAMATERS做到这一点需要信息来进行手动存储?

即:

在.H

  MyClass类
{
    无效美孚(INT一个);
    无效foo2的(双B);
    空巴();
    无效的execute();
    INT _myint;
    双_mydouble;
}

在的.cpp

  MyClass的::巴()
{
    矢量<提高::功能<无效(无效)> myVector;
    myVector.push_back(升压::绑定(放大器; MyClass的:: foo的,这一点,MyClass的:: _敏);
    myVector.push_back(升压::绑定(放大器; MyClass的:: foo2的,这一点,MyClass的:: _ mydouble);
}
MyClass的::执行(字符*参数)
{    提振::函数<无效(无效)> F = myVector [0];
    //魔高一尺这​​里
    //莫名其妙地知道,一个int参数为界
    _myint =的atoi(参数);
    // --------------------------------------
    F();
}


解决方案

据我所知,你不能做你想做的,因为的std ::功能扔掉(几乎)传递函数指针,函数对象或lambda的所有类型的信息。它采用一种称为类型擦除技术和表面上完全忘记了什么传递给它,只要它是可调用与提供的参数。因此,装订后,你的运气似乎的。

然而,则可以提供自己的信息:

 的#include<功能>
#包括LT&;&sstream GT;
#包括LT&;串GT;
#包括LT&;矢量>结构call_info {
  的std ::功能<无效(无效)> FUNC;
  标准::字符串ARG_TYPE;
};MyClass类{
  的std ::矢量<&call_info GT; _myVec;
  INT _myInt;
  双_myDouble;上市:
  无效foo1(int i)以;
  无效foo2的(双D);
  空巴();
  无效的execute(字符*参数);
};无效MyClass的::巴(){
  call_info信息;
  info.func =的std ::绑定(放大器; MyClass的:: foo1,这一点,和放大器; MyClass的:: _敏);
  info.arg_type =INT;
  _myVector.push_back(信息);
  info.func =的std ::绑定(放大器; MyClass的:: foo2的,这一点,和放大器; MyClass的:: _ myDouble);
  info.arg_type =双规;
  _myVector.push_back(信息);
}无效MyClass的::执行(字符*参数){
  call_info&安培;信息= _myVector [0];
  性病:: stringstream的转换次数(参数);  如果(info.arg_type ==INT)
    CONV>> _myInt;
  否则,如果(info.arg_type ==双规)
    CONV>> _myDouble;
  info.func();
}

不是几乎一样干净拥有它自动提供,但据我知道有没有更好的办法(除非像sehe提出彻底改变你的实现)。

Is there any way to retrieve information on what paramaters were bounded by boost::bind or does this need to be stored manually?

i.e.:

in .h

class MyClass
{
    void foo(int a);
    void foo2(double b);
    void bar();
    void execute();
    int _myint;
    double _mydouble;
}

in .cpp

MyClass::bar()
{
    vector<boost::function<void(void)> myVector;
    myVector.push_back(boost::bind(&MyClass::foo, this, MyClass::_myint);
    myVector.push_back(boost::bind(&MyClass::foo2, this, MyClass::_mydouble);
}
MyClass::execute(char* param)
{

    boost::function<void(void)> f  = myVector[0];
    //MAGIC goes here
    //somehow know that an int parameter was bound
    _myint = atoi(param);
    //--------------------------------------
    f();
}

解决方案

As far as I know, you can't do what you want, as std::function throws away (nearly) all type information of the passed function pointer, function object or lambda. It employs a technique called type erasure and on the surface completely forgets what was passed into it, as long as it is callable with the provided arguments. So, after binding, you're out of luck it seems.

However, you can supply that information yourself:

#include <functional>
#include <sstream>
#include <string>
#include <vector>

struct call_info{
  std::function<void(void)> func;
  std::string arg_type;
};

class MyClass{
  std::vector<call_info> _myVec;
  int _myInt;
  double _myDouble;

public:
  void foo1(int i);
  void foo2(double d);
  void bar();
  void execute(char* param);
};

void MyClass::bar(){
  call_info info;
  info.func = std::bind(&MyClass::foo1, this, &MyClass::_myInt);
  info.arg_type = "int";
  _myVector.push_back(info);
  info.func = std::bind(&MyClass::foo2, this, &MyClass::_myDouble);
  info.arg_type = "double";
  _myVector.push_back(info);
}

void MyClass::execute(char* param){
  call_info& info = _myVector[0];
  std::stringstream conv(param);

  if(info.arg_type == "int")
    conv >> _myInt;
  else if(info.arg_type == "double")
    conv >> _myDouble;
  info.func();
}

Not nearly as clean as having it supplied automatically, but as far as I know there's no better way (except completely changing your implementation like sehe proposes).

这篇关于使用boost ::绑定的boost ::功能:获取绑定变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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