Espacios de nombres
Variantes
Acciones

std::mem_fn

De cppreference.com
< cpp‎ | utility‎ | functional
 
 
Biblioteca de servicios
 
Objetos función
Envoltorios de funciones
(C++11)
mem_fn
(C++11)
Aplicación parcial de funciones
(C++20)
(C++11)
Invocación de funciones
(C++17)(C++23)
Objeto función identidad
(C++20)
Envoltorios de referencias
(C++11)(C++11)
Envoltorios de operador transparentes
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
Negadores
(C++17)
Buscadores
Comparadores restringidos
Vinculadores y adaptadores antiguos
(hasta C++17)
(hasta C++17)
(hasta C++17)
(hasta C++17)
(hasta C++17)(hasta C++17)(hasta C++17)(hasta C++17)
(hasta C++20)
(hasta C++20)
(hasta C++17)(hasta C++17)
(hasta C++17)(hasta C++17)

(hasta C++17)
(hasta C++17)(hasta C++17)(hasta C++17)(hasta C++17)
(hasta C++20)
(hasta C++20)
 
Definido en el archivo de encabezado <functional>
template< class R, class T >
/*unspecified*/ mem_fn(R T::* pm);
(1) (desde C++11)
template< class R, class T, class... Args >

/*unspecified*/ mem_fn(R (T::* pm)(Args...));
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) &);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const &);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile &);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile &);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) &&);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const &&);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile &&);
template< class R, class T, class... Args >

/*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile &&);
(2) (c + 11, pero defecto)
std::mem_fn Función plantilla genera objetos envolventes para saber cómo dirigirse a los miembros, que puede almacenar, copiar, e invocar un puntero al miembro. Ambas referencias e indicadores (incluyendo punteros inteligentes) a un objeto se puede utilizar cuando se invoca una std::mem_fn .
Original:
Function template std::mem_fn generates wrapper objects for pointers to members, which can store, copy, and invoke a pointer to member. Both references and pointers (including smart pointers) to an object can be used when invoking a std::mem_fn.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Las sobrecargas (2) se presentan como defecto .
Original:
The overloads (2) are reported as defect.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
El resolution, que recientemente ha sido votada como "Tentativamente Ready" se propone eliminar todas las sobrecargas (2) .
Original:
The resolution, which has recently been voted "Tentatively Ready" proposes to remove all the overloads (2).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Esto romperá algo de código, véase el ejemplo 3 .
Original:
This will break some code, see Example 3.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Contenido

[editar] Parámetros

pm -
puntero al miembro que va a ser envuelto
Original:
pointer to member that will be wrapped
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Valor de retorno

std::mem_fn devuelve una envoltura llamada de tipo no especificado que tiene los miembros siguientes:
Original:
std::mem_fn returns an call wrapper of unspecified type that has 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 :: mem_fn
Original:
std::mem_fn
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 types

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
result_type
el tipo de retorno de pm pm si es un puntero a función miembro, no está definido para el puntero a objeto miembro
Original:
the return type of pm if pm is a pointer to member function, not defined for pointer to member object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
argument_type
T*, posiblemente cv-calificado, si pm es un puntero a función miembro sin argumentos
Original:
T*, possibly cv-qualified, if pm is a pointer to member function taking no arguments
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
T* pm si es un puntero a función miembro toma un argumento
Original:
T* if pm is a pointer to member function taking one argument
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
T1 pm si es un puntero a función miembro toma un argumento de tipo T1
Original:
T1 if pm is a pointer to member function taking one argument of type T1
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Member function

operator()
invoca al destino de un objeto determinado, con parámetros opcionales
Original:
invokes the target on a specified object, with optional parameters
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] Excepciones

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

[editar] Ejemplo 1

Utilice mem_fn para almacenar y ejecutar una función miembro y un objeto miembro:
Original:
Use mem_fn to store and execute a member function and a member object:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <functional>
#include <iostream>
 
struct Foo {
  void display_greeting() {
    std::cout << "Hello, world.\n";
  }
  void display_number(int i) {
    std::cout << "number: " << i << '\n';
  }
  int data = 7;
};
 
int main() {
  Foo f;
 
  auto greet = std::mem_fn(&Foo::display_greeting);
  greet(f);
 
  auto print_num = std::mem_fn(&Foo::display_number);
  print_num(f, 42);
 
  auto access_data = std::mem_fn(&Foo::data);
  std::cout << "data: " << access_data(f) << '\n';
}

Salida:

Hello, world.
number: 42
data: 7

[editar] Ejemplo 2

Pasar una función miembro para std::transform para crear una secuencia de números:
Original:
Pass a member function to std::transform to create a sequence of numbers:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>
#include <functional>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
 
int main()
{
    std::vector<std::string> words = {"This", "is", "a", "test"};
    std::vector<std::unique_ptr<std::string>> words2;
    words2.emplace_back(new std::string("another"));
    words2.emplace_back(new std::string("test"));
 
    std::vector<std::size_t> lengths;
    std::transform(words.begin(),
                   words.end(),
                   std::back_inserter(lengths),
                   std::mem_fn(&std::string::size)); // uses references to strings
    std::transform(words2.begin(),
                   words2.end(),
                   std::back_inserter(lengths),
                   std::mem_fn(&std::string::size)); // uses unique_ptr to strings
 
    std::cout << "The string lengths are ";
    for(auto n : lengths) std::cout << n << ' ';
    std::cout << '\n';
}

Salida:

The string lengths are 4 2 1 4 7 4

[editar] Ejemplo 3

#include <functional>
 
struct X {
  int x;
 
        int& easy()      {return x;}
        int& get()       {return x;}
  const int& get() const {return x;}
};
 
 
int main(void)
{
    auto a = std::mem_fn        (&X::easy); // no problem at all
//  auto b = std::mem_fn<int&  >(&X::get ); // no longer works with new specification
    auto c = std::mem_fn<int&()>(&X::get ); // works with both old and new specification
    auto d = [] (X& x) {return x.get();};   // another approach to overload resolution
}


[editar] Ver también

(C++11)
Envuelve un objeto que puede ser llamado a cualquier tipo con la signatura de llamada a función especificada.
(plantilla de clase) [editar]
(C++11)
Vincula uno o más argumentos a un objeto función.
(plantilla de función) [editar]