std::mem_fn
Defined in header <functional> | | |
---|---|---|
template< class R, class T > /*unspecified*/ mem_fn(R T::* pm); | (1) | (since C++11) |
template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...)); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) const); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) &); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) const &); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile &); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile &); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) &&); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) const &&); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile &&); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile &&); | (2) | (since C++11) (until C++14) |
功能模板std::mem_fn
为指向成员的指针生成包装对象,这些指针可以存储、复制和调用指向成员的指针。引用和指针%28(包括对对象的智能指针%29)都可以在调用std::mem_fn
...
过载%282%29是在C++11中引入的,而在C++14中作为缺陷#2048...
参数
pm | - | pointer to member that will be wrapped |
---|
返回值
std::mem_fn
返回具有下列成员的未指定类型的调用包装器:
STD:MEM[医]FN返回类型
成员类型
type | definition |
---|---|
result_type(deprecated in C++17) | the return type of pm if pm is a pointer to member function, not defined for pointer to member object |
argument_type(deprecated in C++17) | T*, possibly cv-qualified, if pm is a pointer to member function taking no arguments |
first_argument_type(deprecated in C++17) | T* if pm is a pointer to member function taking one argument |
second_argument_type(deprecated in C++17) | T1 if pm is a pointer to member function taking one argument of type T1 |
成员函数
template<class... Args> /* see below */ operator()(Args&&... args); | | |
---|
让fn
的调用返回的调用包装器。std::mem_fn
具有指向成员的指针pm
.然后表达fn(t, a2, ..., aN)
等于INVOKE(pm, t, a2, ..., aN)
,在哪里调用中定义的操作吗?Callable
.%28 Thus,返回类型为operator()
是std::result_of<decltype(pm)(Args&&...)>::type
.%29
每一个论点args
被完美地转发,就好像是...std::forward<Args>(args)...
...
例外
(none). | (until C++17) |
---|---|
noexcept specification: noexcept | (since C++17) |
例1
使用mem_fn
存储和执行成员函数和成员对象:
二次
#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
演示C++14对STD::MEM规范的影响[医]FN。
二次
#include <iostream>
#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 in C++14
auto c = std::mem_fn<int&()>(&X::get ); // works with both C++11 and C++14
auto d = [] (X& x) {return x.get();}; // another approach to overload resolution
X x = {33};
std::cout << "a() = " << a(x) << '\n';
std::cout << "c() = " << c(x) << '\n';
std::cout << "d() = " << d(x) << '\n';
}
二次
产出:
二次
a() = 33
c() = 33
d() = 33
二次
另见
function (C++11) | wraps callable object of any type with specified function call signature (class template) |
---|---|
bind (C++11) | binds one or more arguments to a function object (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com