std::bind
| (1) | ||
template< class F, class... Args > /* */ bind( F&& f, Args&&... args ); |
( C++11) ( C++20) |
|
template< class F, class... Args > constexpr /* */ bind( F&& f, Args&&... args ); |
( C++20) | |
| (2) | ||
template< class R, class F, class... Args > /* */ bind( F&& f, Args&&... args ); |
( C++11) ( C++20) |
|
template< class R, class F, class... Args > constexpr /* */ bind( F&& f, Args&&... args ); |
( C++20) | |
std::is_constructible<std::decay<F>::type, F>::value false std::is_constructible<std::decay<Arg_i>::type, Arg_i>::value false Arg_i Args, .
std::decay<Ti>::type Args MoveConstructible Destructible, .
| f | Callable ( , , , - ), | |
| args | , _1, _2, _3... std::placeholders
|
g T std::is_bind_expression<T>::value true. :
std::bind
-
std::bind - std::decay<F>::type, std::forward<F>(f) args... std::decay<Arg_i>::type, std::forward<Arg_i>(arg_i).
std::bind CopyConstructible, - ( ) CopyConstructible, MoveConstructible . :
|
( C++20) |
operator()
g g(u1, u2, ... uM), ,
INVOKE(fd, std::forward<V1>(v1), std::forward<V2>(v2), ..., std::forward<VN>(vN)), INVOKE<R>(fd, std::forward<V1>(v1), std::forward<V2>(v2), ..., std::forward<VN>(vN)), fd std::decay<F>::type, v1, v2, ..., vN , .
, g(), , g, .
operator() ( C++20) INVOKE. operator() , INVOKE .
g volatile-, .
INVOKE(fd, w1, w2, ..., wN) w1, w2, ..., wN, .
1:
arg_i std::reference_wrapper<T> (, std::ref std::cref std::bind), v_i arg_i.get() V_i T&: .
2:
arg_i T, std::is_bind_expression<T>::value true (, std::bind std::bind), std::bind : , , , . - -, ( u1, u2, ...). , v_i arg_i(std::forward<Uj>(uj)...), V_i std::result_of<T cv &(Uj&&...)>::type&& ( C++17)std::invoke_result_t<T cv &, Uj&&...>&& ( C++17) (cv- , g).
3:
arg_i T, std::is_placeholder<T>::value 0 ( , , std::placeholders::_1, _2, _3, ..., std::bind), , (u1 _1, u2 _2 ..), : v_i std::forward<Uj>(uj), V_i Uj&&.
4:
arg_i lvalue: v_i arg_i, V_i T cv &, cv cv-, g.
, std::decay<F>::type std::forward<F>(f) , std::decay<Arg_i>::type std::forward<Arg_i>(arg_i) , Arg_i i- , arg_i i- Args... args.
Callable, - (, , , std::shared_ptr std::unique_ptr) , .
(, _1) , , (u1) lvalue rvalue.
#include <functional>
#include <iostream>
#include <memory>
#include <random>
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; // for _1, _2, _3...
std::cout << "1) : ";
int n = 7;
// (_1 _2 std::placeholders,
// , f1)
auto f1 = std::bind(f, _2, 42, _1, std::cref(n), n);
n = 10;
f1(1, 2, 1001); // 1 _1, 2 _2, 1001
// f(2, 42, 1, n, 7)
std::cout << "2) : ";
n = 7;
auto lambda = [&ncref=n, n](auto a, auto b, auto /* */) {
f(b, 42, a, ncref, n);
};
n = 10;
lambda(1, 2, 1001); // , f1(1, 2, 1001)
std::cout << "3) : ";
auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5);
f2(10, 11, 12); // f(12, g(12), 12, 4, 5);
std::cout << "4) RNG : ";
std::default_random_engine e;
std::uniform_int_distribution<> d(0, 10);
auto rnd = std::bind(d, e); // e rnd
for(int n=0; n<10; ++n)
std::cout << rnd() << ' ';
std::cout << '\n';
std::cout << "5) -: ";
Foo foo;
auto f3 = std::bind(&Foo::print_sum, &foo, 95, _1);
f3(5);
std::cout << "6) mem_fn, -: ";
auto ptr_to_print_sum = std::mem_fn(&Foo::print_sum);
auto f4 = std::bind(ptr_to_print_sum, &foo, 95, _1);
f4(5);
std::cout << "7) : ";
auto f5 = std::bind(&Foo::data, _1);
std::cout << f5(foo) << '\n';
std::cout << "8) mem_fn, : ";
auto ptr_to_data = std::mem_fn(&Foo::data);
auto f6 = std::bind(ptr_to_data, _1);
std::cout << f6(foo) << '\n';
std::cout << "9) "
" : ";
std::cout << f6(std::make_shared<Foo>(foo)) << ' '
<< f6(std::make_unique<Foo>(foo)) << '\n';
}
:
1) : 2 42 1 10 7
2) : 2 42 1 10 7
3) : 12 12 12 4 5
4) RNG : 0 1 8 5 5 2 0 7 7 10
5) -: 100
6) mem_fn, -: 100
7) : 10
8) mem_fn, : 10
9) : 10 10
C++:
| LWG 2021 | C++11 | 1. fd2. 2, V_i std::result_of<T cv (Uj...)>::type
|
1. 2. std::result_of<T cv &(Uj&&...)>::type&&
|
(C++20)(C++23) |
( ) |
(C++11) |
std::bind () |
(C++11) |
( ) |