简体   繁体   English

boost :: bind用于模板类中的非静态成员函数

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

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;
}

I have a template-d class with a non-static member function intended as comparator for std::sort . 我有一个带有非静态成员函数的template-d类,用作std::sort比较器。 The comparator depends on the typename T parameter. 比较器取决于typename T参数。 As it has an implicit this pointer I try to boost::bind the pointer this to it. 因为它有一个隐含的this指针我试图boost::bind的指针this吧。

Yet neither boost::bind<bool>(.......) nor boost::bind(....) would compile. 但是boost::bind<bool>(.......)boost::bind(....)都不会编译。

The example above fails on MSVC 2008 (as I'm on a non-English environment I'm not sure about the exact message in English, but probably complaining about either prototypes could make all necessary conversions of arguments feasible.) 上面的示例在MSVC 2008上失败(因为我在非英语环境中,我不确定确切的英语消息,但可能抱怨这两个原型都可能使所有必要的参数转换成为可能。)

Well, after quite some digging... The problem indeed does not lie in the snippet presented above. 好吧,经过一番挖掘...问题确实不在于上面介绍的摘录。

Turns out to be an issue similar to ( Strange VC++ compile error, C2244 ) in another member function related. 原来是与另一个成员函数相关的( 奇怪的VC ++编译错误,C2244 )类似的问题。 A function called in compare happened to be a template function that failed to compile exactly like the one in the above question. 称为compare的函数恰好是模板函数,无法像上述问题中那样完全编译。 I didn't notice THAT error at first. 起初我没有注意到那个错误。

I moved parts of code from class.cpp to class.hpp and now it works. 我将部分代码从class.cpp移到了class.hpp ,现在可以使用了。

A stupid MSVC bug, and a stupid mistake I made. 一个愚蠢的MSVC错误,以及一个我犯的愚蠢错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Boost绑定时无效使用非静态成员函数-C ++ - invalid use of non-static member function when using boost bind - c++ 指向模板类的非静态成员函数的C ++函数指针(类成员) - C++ function pointer (class member) to non-static member function of a template class 非静态成员函数的无效使用在类模板的成员函数的实例化中? - Invalid use of non-static member function In instantiation of member function of a class template? 我可以将BOOST_TEST_CASE用于非静态类成员函数吗? - Can I use BOOST_TEST_CASE for non-static class member function? boost :: thread无效使用非静态成员函数 - boost::thread invalid use of a non-static member function boost::signals2 插槽作为非静态 function 成员? - boost::signals2 slot as a non-static function member? 将非静态成员 function 传递给另一个 class 的成员 function - Pass non-static member function to member function of another class 我怎样`std :: bind`一个非静态类成员到Win32回调函数`WNDPROC`? - How do I `std::bind` a non-static class member to a Win32 callback function `WNDPROC`? std :: bind绑定到非静态成员函数会引发很多错误 - std::bind to non-static member function throwing a bunch of errors 将成员函数(非静态)作为参数传递给模板函数 - Passing member function (non-static) as a parameter to template function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM