提升功能问题

时间:2011-03-14 02:57:06

标签: c++ boost

我无法弄清楚这里的代码有什么问题,所以我有人可以帮助我:

在头文件中,我定义了以下函数

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

在类头文件中,我定义了这个typedef:

typedef boost::function<void(instr_ptr, std::vector<ResultBase*>) > GenFunction;
在课堂上,我有两个GenFunction实例

GenFunction Gen;
GenFunction Kill;

我按照以下方式分配给他们

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

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

稍后在我的程序中,我分配给gen如下:

blockSolver.SetGenFunction(GenLiveVar);

其中GenLiveVar是前面提到的函数,blockSolver是持有Gen / Kill的类的实例。在blockSolver中,我执行以下操作:

std::vector<ResultBase*> genList;
Gen(currentBlock->GetInstrPtr(i), &genList);

和GetInstrPtr定义为const instr_ptr GetInstrPtr(int index);

它会产生以下错误(抱歉长度)

    no match for call to '(GenFunction) (const instr_ptr, std::vector<ResultBase*, std::allocator<ResultBase*> >*)'
    /nfs/ug/homes-2/r/rileyjon/ece540/Final/boost/function/function_template.hpp:1007: 
note: candidates are: typename boost::function2<R, T1, T2>::result_type boost::function2<R, T1, T2>::operator()(T0, T1) const [with R = void, T0 = boost::shared_ptr<Instruction>, 
T1 = std::vector<ResultBase*, std::allocator<ResultBase*> >

]

我真的不明白为什么这是一个问题:类型肯定是一样的。一些帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:4)

类型肯定相同。

您传递的参数类型为std::vector<ResultBase*>*(指针):

Gen(currentBlock->GetInstrPtr(i), &genList);
                                  ^

相应参数的类型为std::vector<ResultBase*>(值):

boost::function<void(instr_ptr, std::vector<ResultBase*>)>
                                                       ^

还要注意boost::function之间参数类型的不匹配,它通过值获取第二个参数,以及分配给它的两个函数,它们通过引用获取第二个参数。这可能不会给你你期望的行为。