I’d like to do something like this:
boost::regex re("tryme");
ifstream iss;
iss.open("file.txt");
istream_iterator<string> eos;
istream_iterator<string> iit (iss);
find(iit,eos,bind2nd(boost::regex_match),re));
The errors are following:
Could not find a match for ‘bind2nd<_Fn2,_Ty>(bool (*) (BidiIterator,BidiIterator,match_results &,const basic_regex &,unsigned long))’
Could not find a match for ‘find(istream_iterator,int>,istream_iterator,int>,undefined,regex)’
Could you please help me to do it correctly? Thanks.
The first problem is that
std::find()tries to match values, i.e., you need to replacestd::find()bystd::find_if(). That’s simple to do.The next problem is that
boost::regex_matchisn’t one function but a family of functions.std::bind2nd()has no idea which member of this family you want to match against. Also, the function overload you apparently want to use takes three, not two, arguments: the last argument of typeboost::match_flag_typeis defaulted. I got it to work withstd::bind()using this:If you really want to use
std::bind2nd()it is probably easiest to create a simple forwarding function:The
std::bind2nd()template cannot really work with raw function pointers. This is whystd::ptr_fun()needs to be used. In general, when any of the standard functions called*_fun()need to be used, the fun actually stops: these functions cannot cope with functions taking arguments by reference. Thus,my_regex_match()takes itsstd::stringargument by value:std::bind2nd()would try to create a function object taking a reference to reference as argument otherwise. This is another reason why you might want to usestd::bind()orboost::bind().