简体   繁体   中英

I don't understand ptr_fun

I ran into this ptr_fun thing in one of the c++ code, I tried reading up on it from cplusplus.com, but I honestly can't figure out what this function pointer is suppose to do.

The code of interest is very simple, trims the empty parts off the beginning of a string.

static inline string & trim_beg(string & s) {
  s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun < int, int > (isspace))));
  return s;
}

From Effective STL (Item 40) by Scott Meyers:

The only thing ptr_fun does is make some typedefs available. That's it. These typedefs are required by not1, and that's why applying not1 to ptr_fun but applying not1 to ... directly doesn't work. ... lacks the typedefs that not1 demands.

This is basically the same answer given by Praetorian in comments above but Effective STL provides a more general explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM