名前空間
変種
操作

std::bind

提供: cppreference.com
< cpp‎ | utility‎ | functional


 
 
ユーティリティライブラリ
支援を入力します (basic types, RTTI, type traits)
動的なメモリ管理
エラー処理
プログラムユーティリティ
可変個引数関数
日付と時刻
関数オブジェクト
initializer_list(C++11)
bitset
hash(C++11)
関係演算子
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
ペアとタプル
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
フォワード、スワップ、および移動
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
関数オブジェクト
関数ラッパー
Original:
Function wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
function(C++11)
mem_fn(C++11)
bad_function_call(C++11)
バインド
Original:
Bind
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
bind(C++11)
is_bind_expression(C++11)
is_placeholder(C++11)
_1, _2, _3, ...(C++11)
リファレンス·ラッパー
Original:
Reference wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reference_wrapper(C++11)
ref
cref
(C++11)
(C++11)
オペレーターのラッパー
Original:
Operator wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
否定子
Original:
Negators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
バインダーおよびアダプタは非推奨になりました
Original:
Deprecated binders and adaptors
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unary_function(廃止予定)
binary_function(廃止予定)
ptr_fun(廃止予定)
pointer_to_unary_function(廃止予定)
pointer_to_binary_function(廃止予定)
mem_fun(廃止予定)
mem_fun_t
mem_fun1_t
const_mem_fun_t
const_mem_fun1_t
(廃止予定)
(廃止予定)
(廃止予定)
(廃止予定)
mem_fun_ref(廃止予定)
mem_fun_ref_t
mem_fun1_ref_t
const_mem_fun_ref_t
const_mem_fun1_ref_t
(廃止予定)
(廃止予定)
(廃止予定)
(廃止予定)
binder1st
binder2nd
(廃止予定)
(廃止予定)
bind1st
bind2nd
(廃止予定)
(廃止予定)
 
Defined in header <functional>
template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
(1) (C + + 11以来)
template< class R, class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
(2) (C + + 11以来)
関数テンプレートbindfための転送呼び出しラッパーを生成します。このラッパーを呼び出すとfにバインドされ、その引数の一部とargsを呼び出すことと同じです.
Original:
The function template bind generates a forwarding call wrapper for f. Calling this wrapper is equivalent to invoking f with some of its arguments bound to args.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
内部的には、ラッパーは、メンバーstd::decay<F>::typeから構築型std::forward<F>(f)のオブジェクトと同様にargs...から構築型std::decay<Arg_i>::typestd::forward<Arg_i>(arg_i)それぞれ、ごとに1つのオブジェクトを保持している.
Original:
Internally, the wrapper holds a member object of type std::decay<F>::type constructed from std::forward<F>(f), and one object per each of args..., of type std::decay<Arg_i>::type, similarly constructed from std::forward<Arg_i>(arg_i).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] パラメータ

f -
いくつかの引数にバインドされる呼び出し可能オブジェクト(関数オブジェクト、関数へのポインタは、関数への参照、メンバ関数へのポインタ、またはメンバのデータへのポインタ)
Original:
invocable object (function object, pointer to function, reference to function, pointer to member function, or pointer to member data) that will be bound to some arguments
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
args -
プレースホルダで置き換え_1, _2, _3...バインドされていない引数を使用して、バインドする引数のリスト
Original:
list of arguments to bind, with the unbound arguments replaced by the placeholders _1, _2, _3...
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 値を返します

関数の型が指定されていないのTのオブジェクト、そのstd::is_bind_expression<T>::value == trueため、どのstd::functionに格納することができます。 f、すべてargsは可動であり、そうでなければコピー可能である場合、オブジェクトが移動可能である。型は、以下のメンバーを定義します
Original:
A function object of unspecified type T, for which std::is_bind_expression<T>::value == true, and which can be stored in std::function. The object is movable if f and all args are movable, and is copyable otherwise. The type defines the following members:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

のstd ::バインド
Original:
std::bind
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Return type

Member type result_type

1) If F is a pointer to function or a pointer to member function, result_type is the return type of F. If F is a class type with nested typedef result_type, then result_type is F::result_type. Otherwise no result_type is defined.

2) result_type is exactly R.

Member function operator()

Given an object g obtained from an earlier call to bind, when it is invoked in a function call expression g(u1, u2, ... uM), an invocation of the stored object of type std::decay<F>::type takes place, with arguments defined as follows:

  • If the argument is of type std::reference_wrapper<T> (e.g. std::ref or std::cref) was used in the initial call to bind, then the reference T& stored in the bound argument is passed to the invocable object.
  • If std::is_bind_expression<T>::value == true (i.e. another sbind subexpression was used as an argument in the initial call to bind), then that bind-subexpressin is invoked immediately and its result is passed to the invocable object. If the bind subexpression has any placeholder arguments, they are picked from u1, u2, ....
  • If std::is_placeholder<T>::value != 0 (i.e., _1, _2, _3, ... was used as the argument to the initial call to bind), then the argument indicated by the placeholder (u1 for _1, u2 for _2, etc) is passed to the invocable object as std::forward<Uj>(uj).
  • Otherwise, the stored argument is passed to the invocable object as-is.

If some of the arguments that are supplied in the call to g() are not matched by any placeholders stored in g, the unused arguments are evaluated and discarded.

[編集] 例外

std::decay<F>::typeからstd::forward<F>(f)の建設がcorrspondingstd::decay<Arg_i>::typeからstd::forward<Arg_i>(arg_i)ためにスローされた場合、またはコンストラクタのいずれか場合にだけスローArg_iは、i番目のタイプであるところスローとarg_iArgs... args内のi番目の引数です.
Original:
Only throws if construction of std::decay<F>::type from std::forward<F>(f) throws, or any of the constructors for std::decay<Arg_i>::type from the corrsponding std::forward<Arg_i>(arg_i) throws where Arg_i is the ith type and arg_i is the ith argument in Args... args.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] ノート

bindの引数は、コピーまたは移動し、std::refまたはstd::crefに包まれていない限り、参照によって渡されることはありませんアール.
Original:
The arguments to bind are copied or moved, and are never passed by reference unless wrapped in std::ref or std::cref.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
同じバインド式(複数の_1など)の中にプレースホルダを複製することは許可されていますが、対応する引数が(u1)左辺値または非可動右辺値である場合の結果は定義されているだけでなく.
Original:
Duplicate placeholders in the same bind expression (multiple _1's for example) are allowed, but the results are only well defined if the corresponding argument (u1) is an lvalue or non-movable rvalue.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

#include <random>
#include <iostream>
#include <functional>
 
void f(int n1, int n2, int n3, const int& n4, int n5)
{
    std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n';
}
 
int g(int n1)
{
    return n1;
}
 
struct Foo {
    void print_sum(int n1, int n2)
    {
        std::cout << n1+n2 << '\n';
    }
    int data = 10;
};
 
int main()
{
    using namespace std::placeholders;
 
    // demonstrates argument reordering and pass-by-reference
    int n = 7;
    auto f1 = std::bind(f, _2, _1, 42, std::cref(n), n);
    n = 10;
    f1(1, 2, 1001); // 1 is bound by _1, 2 is bound by _2, 1001 is unused
 
    // nested bind subexpressions share the placeholders
    auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5);
    f2(10, 11, 12);
 
    // common use case: binding a RNG with a distribution
    std::default_random_engine e;
    std::uniform_int_distribution<> d(0, 10);
    std::function<int()> rnd = std::bind(d, e);
    for(int n=0; n<10; ++n)
        std::cout << rnd() << ' ';
    std::cout << '\n';
 
    // bind to a member function
    Foo foo;
    auto f3 = std::bind(&Foo::print_sum, foo, 95, _1);
    f3(5);
 
    // bind to member data
    auto f4 = std::bind(&Foo::data, _1);
    std::cout << f4(foo) << '\n';
}

Output:

2 1 42 10 7
12 12 12 4 5
1 5 0 2 0 8 2 2 10 8
100
10

[編集] も参照してください

std::bind式での非バインド引数のプレースホルダ
Original:
placeholders for the unbound arguments in a std::bind expression
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(定数) [edit]
(C++11)
メンバへのポインタの関数オブジェクトを作成します
Original:
creates a function object out of a pointer to a member
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(関数テンプレート) [edit]