std::mem_fn
Aus cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
<tbody> </tbody>| definiert in Header <functional>
|
||
template< class R, class T > /*unspecified*/ mem_fn(R T::* pm); |
(1) | (seit 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, jedoch Defekt) |
Funktions-Template
std::mem_fn generiert Wrapper Objekte für Zeiger auf Elemente, die gespeichert werden können, kopieren, und rufen Sie einen Zeiger auf ein Element. Beide Referenzen und Zeigern (einschließlich Smart Pointer) zu einem Objekt kann beim Aufrufen eines std::mem_fn werden .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.
You can help to correct and verify the translation. Click here for instructions.
Die Überladungen (2) werden als defekt gemeldet .
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.
You can help to correct and verify the translation. Click here for instructions.
Die resolution, die vor kurzem zum "Versuchsweise Ready" schlägt alle Überladungen (2) entfernen .
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.
You can help to correct and verify the translation. Click here for instructions.
Dies wird einige Code zu brechen, siehe Beispiel 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.
You can help to correct and verify the translation. Click here for instructions.
Parameter
| pm | - | Zeiger auf ein Element, das verpackt werden soll
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. |
Rückgabewert
std::mem_fn gibt einen Anruf Wrapper angegebenen Typ, der die folgenden Mitglieder hat: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.
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
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.
You can help to correct and verify the translation. Click here for instructions.
Member types
Typ
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
|
der Rückgabetyp
pm wenn pm ist ein Zeiger auf Member-Funktion, nicht für Zeiger auf Member-Objekt definiert 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*, möglicherweise cv-qualifiziert, wenn pm ein Zeiger auf Member-Funktion ohne Argumente istOriginal: 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* wenn pm ist ein Zeiger auf Member-Funktion ein ArgumentOriginal: 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 wenn pm ist ein Zeiger auf Member-Funktion ein Argument vom Typ 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() |
ruft das Ziel auf einem bestimmten Objekt, mit optionalen Parametern 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. (öffentliche Elementfunktion) |
Ausnahmen
None .
Original:
None.
The text has been machine-translated via Google Translate.
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.
Beispiel 1
Verwenden
mem_fn zu speichern und auszuführen eine Member-Funktion und ein Mitglied Objekt:
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.
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
Beispiel 2
Übergeben Sie eine Member-Funktion in std::transform eine Folge von Zahlen zu erstellen:
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.
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
Beispiel 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
}
Siehe auch
(C++11) |
wickelt aufrufbare Objekt eines beliebigen Typs mit dem angegebenen Funktion Call-Signatur 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. (Klassen-Template) |
(C++11) |
bindet ein oder mehrere Argumente an eine Funktion Objekt 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. (Funktions-Template) |