Why can't mem_fn be used for a member function from std :: string?

struct int_holder {
    int value;
    int triple() {return value*3;}
};

int main(int argc, const char * argv[])
{
    std::string abc{"abc"};
    int_holder one{1};

    auto f1 = mem_fn(&std::string::clear);
    auto f2 = mem_fn(&int_holder::triple);
    f1(abc);
    f2(one);
}

I am testing such code in Xcode and the compiler throws this error enter image description here

Mem_fn seems to work great with user-class member functions, but not standard-line member functions, which are different, and why? thanks for reading, help me plz!

+4
source share
2 answers

As for the standard, you cannot take a pointer to any standard non-static member, because it is allowed to add hidden overloads, default template parameters, SFINAE in the return type to the library implementation, etc.

In other words, & std::string::clearjust not supported.

Clang . () , , . : & std::string::clear , . , , , std::string libc++.so. std::basic_string .

. []( std::string & o ) { o.clear(); } mem_fn. , sizeof . , mem_fn, .

0

Clang 3.1-3.3, 3.6. 16478.

- . extern

#ifndef _LIBCPP_EXTERN_TEMPLATE
#define _LIBCPP_EXTERN_TEMPLATE(...)
#endif

- ( r189610); - (template void std::string::clear();), .

, - . [Member.functions]/2

- :

  • -; 187
  • - - ;
  • - -.

187) , - ++ .

+2

All Articles