std::mem_fun_ref
來自 cppreference.com
定義於標頭檔案 <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() 的第一個引數。
此函式和相關型別在 C++11 中被廢棄,並在 C++17 中移除,取而代之的是更通用的 std::mem_fn 和 std::bind,它們都從成員函式建立可呼叫介面卡相容的函式物件。
目錄 |
[編輯] 引數
f | - | 指向要為其建立包裝器的成員函式的指標 |
[編輯] 返回值
包裝 f 的函式物件。
[編輯] 異常
可能丟擲實現定義的異常。
[編輯] 注意
std::mem_fun 和 std::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 中已移除) |
從成員函式指標建立包裝器,可使用物件指標呼叫 (函式模板) |