名稱空間
變體
操作

std::mem_fun_ref

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

mem_fun_ref
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <functional>
template< class Res, class T >
std::mem_fun_ref_t<Res,T> mem_fun_ref( Res (T::*f)() );
(1) (C++11 起廢棄)
(在 C++17 中已移除)
template< class Res, class T >
std::const_mem_fun_ref_t<Res,T> mem_fun_ref( Res (T::*f)() const );
(1) (C++11 起廢棄)
(在 C++17 中已移除)
template< class Res, class T, class Arg >
std::mem_fun1_ref_t<Res,T,Arg> mem_fun_ref( Res (T::*f)(Arg) );
(2) (C++11 起廢棄)
(在 C++17 中已移除)
template< class Res, class T, class Arg >
std::const_mem_fun1_ref_t<Res,T,Arg> mem_fun_ref( Res (T::*f)(Arg) const );
(2) (C++11 起廢棄)
(在 C++17 中已移除)

建立成員函式包裝器物件,從模板引數推斷目標型別。包裝器物件期望一個型別為 T 的物件的引用作為其 operator() 的第一個引數。

1) 等效地呼叫 std::mem_fun_ref_t<S,T>(f)std::const_mem_fun_ref_t<S,T>(f)
2) 等效地呼叫 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 - 指向要為其建立包裝器的成員函式的指標

[編輯] 返回值

包裝 f 的函式物件。

[編輯] 異常

可能丟擲實現定義的異常。

[編輯] 注意

std::mem_funstd::mem_fun_ref 之間的區別在於,前者生成一個期望物件指標的函式包裝器,而後者期望引用。

[編輯] 示例

使用 std::mem_fun_ref 繫結 std::string 的成員函式 size()

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
 
int main()
{
    std::vector<std::string> v = {"once", "upon", "a", "time"};
    std::transform(v.cbegin(), v.cend(),
                   std::ostream_iterator<std::size_t>(std::cout, " "),
                   std::mem_fun_ref(&std::string::size));
}

輸出

4 4 1 4

[編輯] 參閱

(C++11 中已廢棄)(C++17 中已移除)
從成員函式指標建立包裝器,可使用物件指標呼叫
(函式模板) [編輯]