std::mem_fn
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Elemento definito nell'header <functional>
|
||
template< class R, class T > /*unspecified*/ mem_fn(R T::* pm); |
(1) | (dal C++11) |
template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...)); |
(2) | (c + 11, ma difetto) |
std::mem_fn
funzione genera oggetti wrapper per i puntatori ai membri, in grado di memorizzare, copiare, e richiamare un puntatore a membro. Entrambi i riferimenti e puntatori (compresi i puntatori intelligenti) a un oggetto può essere utilizzato quando si invoca un std::mem_fn
.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
.You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica] Parametri
pm | - | puntatore al membro che verrà avvolto
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. |
[modifica] Valore di ritorno
std::mem_fn
restituisce una fascia chiamata di tipo non specificato, che ha i seguenti membri:std::mem_fn
returns an call wrapper of unspecified type that has the following members:You can help to correct and verify the translation. Click here for instructions.
std :: mem_fnOriginal:std::mem_fnThe 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 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
|
il tipo di ritorno di pm se pm è un puntatore a funzione membro, non definito per puntatore a oggetto membro
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* , possibilmente cv-qualificata, se pm è un puntatore a funzione membro prendere senza argomentiOriginal: T* , possibly cv-qualified, if pm is a pointer to member function taking no argumentsThe 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* se pm è un puntatore a funzione membro prendere un argomentoOriginal: T* if pm is a pointer to member function taking one argumentThe 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 se pm è un puntatore a funzione membro di prendere un argomento di T1 tipo 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 l'obiettivo su un oggetto specificato, con i parametri opzionali 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. (metodo pubblico) |
[modifica] Eccezioni
You can help to correct and verify the translation. Click here for instructions.
[modifica] Esempio 1
mem_fn
per memorizzare ed eseguire una funzione di membro e un oggetto membro:
mem_fn
to store and execute a member function and a member object:
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'; }
Output:
Hello, world. number: 42 data: 7
[modifica] Esempio 2
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'; }
Output:
The string lengths are 4 2 1 4 7 4
[modifica] Esempio 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 }
[modifica] Vedi anche
(C++11) |
avvolge oggetto invocabile di qualsiasi tipo con la firma specificato chiamata di funzione Original: wraps callable object of any type with specified function call signature The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (classe template) |
(C++11) |
lega uno o più argomenti a un oggetto funzione Original: binds one or more arguments to a function object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) |