【问题标题】:boost::bind for a non-static member function in a template classboost::bind 用于模板类中的非静态成员函数
【发布时间】:2013-02-16 02:30:47
【问题描述】:
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 指针,所以我尝试将boost::bind 指针this 指向它。

然而boost::bind&lt;bool&gt;(.......)boost::bind(....) 都不会编译。

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

【问题讨论】:

  • 请贴出错误信息,必要时使用在线编译器获取英文信息
  • 并发布您尝试过的代码,(.....) 没有帮助。
  • this 编译,但我不知道它是否与您的代码相同。
  • 谢谢。原来是在另一个相关的成员函数中类似于link 的问题。我将部分代码从class.cpp 移动到class.hpp,现在它可以工作了。
  • @Phoeagon 如果您解决了问题,请回复您的问题

标签: c++ templates boost stl bind


【解决方案1】:

好吧,经过一番挖掘......问题确实不在于上面介绍的sn-p。

原来是与另一个相关成员函数中的 (Strange VC++ compile error, C2244) 类似的问题。在compare 中调用的函数恰好是一个模板函数,它无法像上述问题中的那样编译。一开始我没有注意到那个错误。

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

一个愚蠢的 MSVC 错误,我犯了一个愚蠢的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多