std::mem_fn
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <functional> 中定义
|
||
template< class R, class T > /*unspecified*/ mem_fn(R T::* pm); |
(1) | (C++11 起) |
template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...)); |
(2) | (C + +11,但缺陷) |
std::mem_fn
生成包装对象指向成员的指针,它可以存储,复制和引用指向成员的指针。两个引用和指针(包括智能指针)的对象时,可以使用调用std::mem_fn
.std::mem_fn
generates wrapper objects for pointers to members, which can store, copy, and invoke a pointer to member. Both references and pointers (including smart pointers) to an object can be used when invoking a std::mem_fn
.目录 |
[编辑] 参数
pm | - | 指针成员将包裹
原文: pointer to member that will be wrapped |
[编辑] 返回值
std::mem_fn
返回一个呼叫的类型不明确,它具有以下成员的包装std::mem_fn
returns an call wrapper of unspecified type that has the following members: 的std :: mem_fn的 Return type
Member types
类型
|
definition |
result_type
|
pm如果pm的返回类型是一个指向成员函数的指针,而不是定义为指针成员对象
原文: the return type of pm if pm is a pointer to member function, not defined for pointer to member object |
argument_type
|
T* ,可能是合格的简历,如果pm是一个指向成员函数没有参数原文: T* , possibly cv-qualified, if pm is a pointer to member function taking no arguments |
first_argument_type
|
T* 如果pm是一个参数的成员函数的指针原文: T* if pm is a pointer to member function taking one argument |
second_argument_type
|
T1 如果pm是一个指向成员函数的指针类型T1 的一个参数 原文: T1 if pm is a pointer to member function taking one argument of type T1 |
Member function
operator() |
调用指定对象上的目标,可选的参数 原文: invokes the target on a specified object, with optional parameters (公共成员函数) |
[编辑] 例外
[编辑] 实施例1
mem_fn
储存和执行一个成员函数和一个成员对象:
mem_fn
to store and execute a member function and a member object:
#include <functional> #include <iostream> struct Foo { void display_greeting() { std::cout << "Hello, world.\n"; } void display_number(int i) { std::cout << "number: " << i << '\n'; } int data = 7; }; int main() { Foo f; auto greet = std::mem_fn(&Foo::display_greeting); greet(f); auto print_num = std::mem_fn(&Foo::display_number); print_num(f, 42); auto access_data = std::mem_fn(&Foo::data); std::cout << "data: " << access_data(f) << '\n'; }
输出:
Hello, world. number: 42 data: 7
[编辑] 实施例2
#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'; }
输出:
The string lengths are 4 2 1 4 7 4
[编辑] 实施例3
#include <functional> struct X { int x; int& easy() {return x;} int& get() {return x;} const int& get() const {return x;} }; int main(void) { auto a = std::mem_fn (&X::easy); // no problem at all // auto b = std::mem_fn<int& >(&X::get ); // no longer works with new specification auto c = std::mem_fn<int&()>(&X::get ); // works with both old and new specification auto d = [] (X& x) {return x.get();}; // another approach to overload resolution }
[编辑] 另请参阅
(C++11) |
包装任何类型的可调用对象与指定的函数调用签名 原文: wraps callable object of any type with specified function call signature (类模板) |
(C++11) |
绑定一个或多个参数的函数对象 原文: binds one or more arguments to a function object (函数模板) |