Namespaces
Variants

std:: mem_fun_ref

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

mem_fun_ref
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until 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で削除)

メンバ関数のラッパーオブジェクトを作成し、テンプレート引数からターゲット型を推論します。ラッパーオブジェクトは、その operator ( ) の最初のパラメータとして、型 T のオブジェクトへの参照を期待します。

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_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で削除)
メンバ関数へのポインタから、オブジェクトへのポインタで呼び出し可能なラッパーを作成する
(関数テンプレート)