[英]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.