Trying to get past std::bind compile errors

时间:2018-09-22 22:41:42

标签: c++ stdbind

I've used std::bind before and I think am close on this usage but not quite there and I don't have a clue how to resolve the compile error.

The ultimate goal is a medium-sized array of pointers to a small number of functions, with different parameters in each array element. At this point I just have one function and one table entry. If I can get that right I think I can solve the rest. I want to use std::function so that I can put the varied parameters into the array.

Here's declaration of the one function so far:

static Get *MakeGArrayStatic(void *Subscript, const void **array, unsigned int sizeOfArray);

Here's the declaration of the single pointer that will be typical of the array:

typedef std::tr1::function<Get *(void *, const void**, unsigned int)> GetMaker;
static GetMaker *gm1;

Here's the definition of the pointer:

Get::GetMaker *Get::gm1 = std::tr1::bind(&MakeGArrayStatic, &OutMsg::CurrentSeverity, FacSevTbls::SyslogSeveritiesForMessages, FacSevTbls::NumberOfTrueSeverities);

(Get is a class, CurrentSeverity is an enum, SyslogSeveritiesForMessages is a const char **, and NumberOfTrueSeverities is a size_t.)

The error I am getting (VS 2010) is

error C2440: 'initializing' : cannot convert from 'std::tr1::_Bind<_Result_type,_Ret,_BindN>' to 'Get::GetMaker *' with [ _Result_type=Get *, _Ret=Get *, _BindN=std::tr1::_Bind3,SyslogEnums::SeverityEnum *,const char **,size_t> ] No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Can anyone please point out where I am going wrong?

1 个答案:

答案 0 :(得分:0)

再次感谢您@PlinyTheElder,但我认为我们没有“正式”回答这个问题。关闭循环,以下是在函数数组中的第一个剪切点的声明:

static Get *MakeGArrayStatic(void *Subscript, const char **array, size_t sizeOfArray);
static Get *MakeGStatic(void *field, size_t sizeOfField);
typedef std::tr1::function<Get *()> GetMaker;
static GetMaker gm[];

这是到目前为止数组的定义:

std::tr1::function<Get *()> Get::gm[] = { 
    std::tr1::bind(&Get::MakeGArrayStatic, &OutMsg::CurrentSeverity, FacSevTbls::SyslogSeveritiesForMessages, FacSevTbls::NumberOfTrueSeverities), 
    std::tr1::bind(&MakeGStatic, Msg::MessageID, 8) } ;

这是一个简单的函数调用示例:

    Get *g = Get::gm[0]();

出来比我期望的要好。我曾经想象过,所有“小功能”都必须具有相同的签名(如重载)。在两个目标编译器上都可以干净地编译。 (尚未测试执行情况,但我有信心。)再次感谢!

更新:是的,它会执行。