首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::mem_fun_ref

Defined in header <functional>

template< class Res, class T > std::mem_fun_ref_t<Res,T> mem_fun_ref( Res (T::*f)() );

(1)

(until C++17)(deprecated since C++11)

template< class Res, class T > std::const_mem_fun_ref_t<Res,T> mem_fun_ref( Res (T::*f)() );

(1)

(until C++17)(deprecated since C++11)

template< class Res, class T, class Arg > std::mem_fun1_ref_t<Res,T,Arg> mem_fun_ref( Res (T::*f)(Arg) );

(2)

(until C++17)(deprecated since C++11)

template< class Res, class T, class Arg > std::const_mem_fun1_ref_t<Res,T,Arg> mem_fun_ref( S (T::*f)(Arg) );

(2)

(until C++17)(deprecated since C++11)

创建成员函数包装对象,从模板参数推断目标类型。包装器对象需要对类型对象的引用。T作为它的第一个参数operator()...

1%29有效呼叫std::mem_fun_ref_t<S,T>(f)std::const_mem_fun_ref_t<S,T>(f)...

2%29有效呼叫std::mem_fun1_ref_t<S,T>(f)std::const_mem_fun1_ref_t<S,T>(f)...

这个函数和相关的类型在C++11中被取消,在C++17中被删除,以支持更一般的std::mem_fnstd::bind,它们都从成员函数创建可调用适配器兼容的函数对象。

参数

f

-

pointer to a member function to create a wrapper for

返回值

函数对象包装f...

例外

%280%29

注记

之间的区别std::mem_funstd::mem_fun_ref前者产生一个函数包装器,期望一个指向对象的指针,而后者--一个引用。

用途STD::MEM[医]乐趣[医]引用绑定std::string%27s成员函数.size%28%29。

二次

代码语言:javascript
复制
#include <functional>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <iostream>
 
int main()
{
    std::vector<std::string> v = {"once", "upon", "a", "time"};
    std::transform(v.begin(), v.end(),
                   std::ostream_iterator<std::size_t>(std::cout, " "),
                   std::mem_fun_ref(&std::string::size));
}

二次

产出:

二次

代码语言:javascript
复制
4 4 1 4

二次

另见

mem_fun (until C++17)

creates a wrapper from a pointer to member function, callable with a pointer to object (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券