#include #include #include int main() { auto f = std::mem_fn(&std::string::size); std::string woerter[] = { "Ein", "Test" }; for(auto e : woerter) { std::cout << e << " : " << f(e) << '\n'; } // === überladene Methoden mit cast spezifiziert: // std::string& (std::string::*member)(const std::string&)> = auto member = static_cast(&std::string::append); auto fn = std::mem_fn(member); // std::function auto binder = std::bind(fn, std::placeholders::_1, " and bind"); // === bind() auch ohne mem_fn() möglich: // std::function auto binder2 = std::bind(member, std::placeholders::_1, std::placeholders::_2); // === oder Lambda-Ausdruck: auto lambda = [](std::string& s, const std::string& appendix) -> std::string& { return s.append(appendix); }; // trailing return type needed here, otherwise std::function<> cannot resolve // std::function auto f = std::function(lambda); std::string s = "Hello"; (s.*member)(" world"); fn(s, " of mem_fn"); binder(s); binder2(s, " with placeholders"); lambda(s, " or lambda"); f(s, " as function"); std::cout << s << '\n'; }