std::mem_fn
From Cppreference
C++ Standard Library | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Utilities library | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Function objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <functional>
|
||
template< class R, class T > /*unspecified*/ mem_fn(R T::* pm);
template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...)); |
(since C++11) | |
Function template std::mem_fn generates wrapper objects for pointers to member functions, which can store, copy, and invoke a pointer to member function. Both references and pointers (including smart pointers) to an object can be used when invoking a std::mem_fn..
Contents |
[edit] Parameters
pm | - | pointer to member function that will be wrapped |
[edit] Return value
std::mem_fn returns an object of unspecified simple call wrapper type, which has the following members:
[edit] Member types
type | definition |
result_type | R |
argument_type | T*, possibly cv-qualified, if sizeof...(Args}==0 |
first_argument_type | T* if sizeof...(Args}==1 |
second_argument_type | T1 if sizeof...(Args}==1 and T1 is the only type in Args... |
[edit] Member functions
invokes the target of an std::mem_fn (public member function) |
[edit] Execptions
None.
[edit] Example
#include <iostream> #include <functional> #include <iterator> #include <memory> #include <string> #include <vector> #include <algorithm> int main() { std::vector<std::string> words = {"This", "is", "a", "test"}; std::vector<std::unique_ptr<std::string>> words2; words2.emplace_back(new std::string("another")); words2.emplace_back(new std::string("test")); std::vector<std::size_t> lengths; std::transform(words.begin(), words.end(), std::back_inserter(lengths), std::mem_fn(&std::string::size)); // uses references to strings std::transform(words2.begin(), words2.end(), std::back_inserter(lengths), std::mem_fn(&std::string::size)); // uses unique_ptr to strings std::cout << "The string lengths are "; for(auto n : lengths) std::cout << n << ' '; std::cout << '\n'; }
Output:
The string lengths are 4 2 1 4 7 4
[edit] See also
(C++11)
|
wraps callable object of any type with specified function call signature (class template) |
(C++11)
|
binds one or more arguments to a function object (function template) |