Error

C++博客 首页 新随笔 联系 聚合 管理
  217 Posts :: 61 Stories :: 32 Comments :: 0 Trackbacks

// 如果参数为const int&类型,就会挂掉。据说是编译器实现的时候忽略了?
// 具体分析错误应该是这样: std::ptr_fun会构造出一个派生于binary_function的对象,
// 传递给他的模板参数就是函数参数类型,如果传递&类型,会导致调用真是函数时候
// argument_type&变成argument_type&&引发编译错误,除非能在std::prt_fun中推导出
// Val&参数类型中的Val类型作为模板参数传递下去
bool Cmp(const int& iLeft, const int& iRight)
{
    return true;
}

// std::binary_functiond在传递函数参数的时候已经分别生命了const TVal& 和 TVal&两个版本,
// 所以在实例化的时候不能传递const TVal&上去,会造成编译错误
class Functor_Cmp : public std::binary_function<int, int, bool>
{
public:
    bool operator () (const int& iLeft, const int& iRight) const
    {
        return true;
    }
};

void Test_Bind2end()
{
    vector<int> vInt(9);

    // 注意functor 和function ptr的区别
    std::count_if(vInt.begin(), vInt.end(), std::bind2nd(std::ptr_fun(&Cmp), 1));
    std::count_if(vInt.begin(), vInt.end(), std::bind2nd(Functor_Cmp(), 1));
}

posted on 2013-10-04 16:21 Enic 阅读(1437) 评论(1)  编辑 收藏 引用 所属分类: 理解stl

评论

# re: std::bind2nd简单理解 2014-03-16 19:02 Enic
1.std::bind2nd std::bind1st 用于参数绑定
2.std::binary_function 等用于支持std::bind2nd std::bind1st
3.std::mem_fun1用于从普通函数构造出派生于binary_function临时对象支持bind系列  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理