Megatron
Megatron

Reputation: 2949

C++ boost function issue

I did something to break the functionality in my program, but I can't figure out what. I define a typedef in a class headerfile:

typedef boost::function<void(instr_ptr, std::vector<ResultBase*>) > GenFunction;

And inside that class I have two instances:

GenFunction Gen;
GenFunction Kill

I set them as follows:

void DataFlowSolver::SetGenFunction(GenFunction &func)
{
    Gen = func;
}

void DataFlowSolver::SetKillFunction(GenFunction &func)
{
    Kill = func;
}

I have another function in a seperate header file:

void GenLiveVar(const instr_ptr instr, std::vector<ResultBase*> &list);

I create an instance of the DataFlowSolver class, and attempt to assign into it as follows:

blockSolver.SetGenFunction(GenLiveVar);

However, the compiler complains:

CFG.cc:617: error: no matching function for call to 'DataFlowSolver::SetGenFunction(void (&)(instr_ptr, std::vector >&))' DataFlowSolver.h:21: note: candidates are: void DataFlowSolver::SetGenFunction(GenFunction&)

But it lets me do this:

GenFunction fun = GenLiveVar;
blockSolver.SetGenFunction(fun);

Anyone have an idea what might be wrong? I know this worked before, but I'm not sure how I managed to break it...

Upvotes: 0

Views: 119

Answers (1)

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

You are passing the boost::function into Set*Function by non-const reference. That prevents temporaries from being used as arguments, and the conversion from a normal function to a boost::function creates a temporary value. You will need to use a const reference for your parameter type for the code to work correctly.

Upvotes: 1

Related Questions