◐ Shell
clean mode source ↗

std::reference_wrapper - cppreference.com

De cppreference.com

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 T > class reference_wrapper;

(desde C++11)

std::reference_wrapper clase de plantilla es una CopyConstructible y la envoltura alrededor CopyAssignable una referencia al objeto o referencia a funcionar de T tipo. Las instancias de std::reference_wrapper son objetos (puede ser copiado o almacenado en contenedores), pero son implícitamente convertible a T&, de modo que puedan ser utilizados como argumentos con las funciones que toman el tipo subyacente por referencia .

Original:

Class template std::reference_wrapper is a CopyConstructible and CopyAssignable wrapper around a reference to object or reference to function of type T. Instances of std::reference_wrapper are objects (can be copied or stored in containers), but they are implicitly convertible to T&, so that they can be used as arguments with the functions that take the underlying type by reference.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Las funciones de ayuda std::ref y std::cref se utilizan a menudo para generar std::reference_wrapper objetos .

Original:

Helper functions std::ref and std::cref are often used to generate std::reference_wrapper objects.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

std::reference_wrapper también se utiliza para pasar objetos a std::bind o al constructor de std::thread por referencia .

Original:

std::reference_wrapper is also used to pass objects to std::bind or to the constructor of std::thread by reference.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Tipos de miembros

tipo

Original:

type

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

definition
type T
result_type

El tipo de retorno de T si T es una función. De lo contrario, no definida

Original:

The return type of T if T is a function. Otherwise, not defined

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

argument_type

1) si T es una función o un puntero a una función que toma un argumento de tipo A1, entonces argument_type es A1.

2) si T es un puntero a función miembro de clase T0 que no toma ningún argumento, entonces es argument_type T0*, posiblemente cv-qualified

3) si T es un tipo de clase con un T::argument_type tipo de miembro, entonces argument_type es un alias de eso

Original:

1) if T is a function or pointer to function that takes one argument of type A1, then argument_type is A1.

2) if T is a pointer to member function of class T0 that takes no arguments, then argument_type is T0*, possibly cv-qualified

3) if T is a class type with a member type T::argument_type, then argument_type is an alias of that

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

first_argument_type

1) si T es una función o un puntero a una función que toma dos argumentos de tipo s A1 A2 y, a continuación, first_argument_type es A1.

2) si T es un puntero a función miembro de clase T0 que toma un argumento, entonces es first_argument_type T0*, posiblemente cv-qualified
3) si T es un tipo de clase con un T::first_argument_type tipo de miembro, entonces first_argument_type es un alias de eso

Original:

1) if T is a function or pointer to function that takes two arguments of type s A1 and A2, then first_argument_type is A1.

2) if T is a pointer to member function of class T0 that takes one argument, then first_argument_type is T0*, possibly cv-qualified
3) if T is a class type with a member type T::first_argument_type, then first_argument_type is an alias of that

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

second_argument_type

1) si T es una función o un puntero a una función que toma dos argumentos de tipo s A1 A2 y, a continuación, second_argument_type es A2.

2) si T es un puntero a la función miembro de T0 clase que toma un argumento A1, entonces second_argument_type es A1, posiblemente cv-qualified

3) si T es un tipo de clase con un T::second_argument_type tipo de miembro, entonces first_argument_type es un alias de eso

Original:

1) if T is a function or pointer to function that takes two arguments of type s A1 and A2, then second_argument_type is A2.

2) if T is a pointer to member function of class T0 that takes one argument A1, then second_argument_type is A1, possibly cv-qualified

3) if T is a class type with a member type T::second_argument_type, then first_argument_type is an alias of that

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Las funciones miembro

almacena una referencia a un objeto std::reference_wrapper nuevo

Original:

stores a reference in a new std::reference_wrapper object

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(función miembro pública) [editar]

vuelve a enlazar una std::reference_wrapper

Original:

rebinds a std::reference_wrapper

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(función miembro pública) [editar]

accede a la referencia almacenada

Original:

accesses the stored reference

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(función miembro pública) [editar]
llama a la función almacenada
(función miembro pública) [editar]

Ejemplo

Muestra el uso de reference_wrapper como un contenedor de referencias, lo que hace posible acceder al mismo recipiente utilizando varios índices

Original:

Demonstrates the use of reference_wrapper as a container of references, which makes it possible to access the same container using multiple indexes

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <functional>

int main()
{
    std::list<int> l = {-4, -3, -2, -1, 0, 1, 2, 3, 4};
 
    std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
    std::random_shuffle(v.begin(), v.end());

    std::vector<std::reference_wrapper<int>> v2(v.begin(), v.end());
    std::partition(v2.begin(), v2.end(), [](int n){return n<0;});

    std::cout << "Contents of the list: ";
    for(int n: l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';

    std::cout << "Contents of the list, shuffled: ";
    for(int i: v) {
        std::cout << i << ' ';
    }
    std::cout << '\n';

    std::cout << "Shuffled elements, partitioned: ";
    for(int i: v2) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
}

Salida:

Contents of the list: -4 -3 -2 -1 0 1 2 3 4 
Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2 
Shuffled elements, partitioned: -3 -1 -2 -4 4 1 3 0 2

Ver también

Crea un envoltorio de referencia (std::reference_wrapper) con un tipo deducido de su argumento.
(plantilla de función) [editar]
Vincula uno o más argumentos a un objeto función.
(plantilla de función) [editar]