boost::为模板类中的非静态成员函数绑定

boost::bind for a non-static member function in a template class

本文关键字:静态成员 绑定 函数 boost      更新时间:2023-10-16
template<typename T> class testClass{
    public:
    bool compare(const int& a, const int& b){
        T x;
        ....
    }
    void sort(){
        std::sort( data.begin() ,
                data.end() ,
                boost::bind<bool>(
                &testClass<T>::compare,
                this, _1 , _2 ) );
    }
    std::vector<int> data;
}

我有一个template-d类,它具有一个非静态成员函数,用作std::sort的比较器。比较器取决于typename T参数。由于它有一个隐含的this指针,我尝试将指针this指向boost::bind

然而boost::bind<bool>(.......)boost::bind(....)都不会编译。

上面的例子在MSVC 2008上失败了(因为我在一个非英语环境中,我不确定英语中的确切消息,但可能抱怨任何一个原型都会使所有必要的参数转换变得可行。(

好吧,经过一番挖掘。。。问题确实不在于上面的片段。

事实证明,在另一个成员函数中存在类似于(Strange VC++编译错误,C2244(的问题。compare中调用的一个函数恰好是一个模板函数,未能像上面问题中那样编译。一开始我没有注意到那个错误。

我将部分代码从class.cpp移到了class.hpp,现在它可以工作了。

一个愚蠢的微软风投错误,也是我犯的一个愚蠢错误。