0
votes

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
Why not using lambda functions instead of std::bind at all?πάντα ῥεῖ
why is gm1 a pointer?kmdreko
Because I am targeting two compilers and the other one sadly does not support lambda functions. (Neither does VS 2010, but it would be easy to upgrade VS. Not so the other compiler.) But good suggestion. Thanks.Charles
But std::bind() does not return a pointer, does it? It returns a callable object by value (that you can assign to your std::function()).PlinyTheElder
@Charles: no no, it should be GetMaker func = std::bind( ...your params here... ). And then you have to get the parameters right.PlinyTheElder

1 Answers

0
votes

Thank you again to @PlinyTheElder but I think we have left the question "officially" unanswered. Closing the loop, here are the declarations with the first cut at an array of functions:

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[];

and here is the definition of the array so far:

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

And here is a trivial example of a call to a function:

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

Came out better than I had hoped. I had pictured that all of the "little functions" were going to have to have the same signature (like overloads). Compiles cleanly on both target compilers. (Have not tested execution yet, but I am confident.) Thanks again!

Update: yes, it executes.