std::bind
|
|
Esta página se ha traducido por ordenador/computador/computadora de la versión en inglés de la Wiki usando Google Translate.
La traducción puede contener errores y palabras aparatosas/incorrectas. Planea sobre el texto para ver la versión original. Puedes ayudar a corregir los errores y mejorar la traducción. Para instrucciones haz clic aquí. |
| Definido en el archivo de encabezado <functional>
|
||
template< class F, class... Args > /*unspecified*/ bind( F&& f, Args&&... args ); |
(1) | (desde C++11) |
template< class R, class F, class... Args > /*unspecified*/ bind( F&& f, Args&&... args ); |
(2) | (desde C++11) |
bind plantilla de función genera un contenedor de desvío de llamadas para f. Llamar a este envoltorio es equivalente a invocar f con algunos de sus argumentos vinculados a args . bind generates a forwarding call wrapper for f. Calling this wrapper is equivalent to invoking f with some of its arguments bound to args. You can help to correct and verify the translation. Click here for instructions.
std::decay<F>::type tipo construido a partir std::forward<F>(f), y un objeto por cada uno de args..., de std::decay<Arg_i>::type tipo, de forma similar construida a partir de std::forward<Arg_i>(arg_i) .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).You can help to correct and verify the translation. Click here for instructions.
Parámetros
| f | - | objeto invocable (objeto función, puntero a función, la referencia a la función, puntero a función miembro, o un puntero a los datos miembros) que se enlazará a algunos argumentos
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 | - | lista de argumentos para atar, con los argumentos no consolidados sustituyen por el
_1, _2, _3... marcadores de posición 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. |
Valor de retorno
T tipo no especificado, para que std::is_bind_expression<T>::value == true, y que puede ser almacenado en std::function. El objeto es móvil si f y args todos son móviles, y es copiable lo contrario. El tipo define por los siguientes miembros: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:You can help to correct and verify the translation. Click here for instructions.
std :: bindOriginal:std::bindThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. Return type
You can help to correct and verify the translation. Click here for instructions.
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 tobind, then the referenceT&stored in the bound argument is passed to the invocable object. - If
std::is_bind_expression<T>::value == true(i.e. anothersbindsubexpression was used as an argument in the initial call tobind), 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 fromu1, u2, .... - If
std::is_placeholder<T>::value != 0(i.e.,_1, _2, _3, ...was used as the argument to the initial call tobind), then the argument indicated by the placeholder (u1for_1,u2for_2, etc) is passed to the invocable object asstd::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.
Excepciones
std::decay<F>::type de std::forward<F>(f) tiros, o cualquiera de los constructores para std::decay<Arg_i>::type del std::forward<Arg_i>(arg_i) # correspondientes arroja donde Arg_i es el tipo iy arg_i es el argumento i en Args... args .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.You can help to correct and verify the translation. Click here for instructions.
Notas
You can help to correct and verify the translation. Click here for instructions.
_1, por ejemplo) están permitidos, pero los resultados sólo están bien definidos si el argumento correspondiente (u1) es un valor-valor-o no-móvil ._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.You can help to correct and verify the translation. Click here for instructions.
Ejemplo
#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';
}
Salida:
2 1 42 10 7
12 12 12 4 5
1 5 0 2 0 8 2 2 10 8
100
10
Ver también
(C++11) |
Marcadores de posición para los argumentos no vinculados en una expresión std::bind. (constante) |
(C++11) |
Crea un objeto función de un puntero a un miembro. (plantilla de función) |