kennen:lib:bind
Inhaltsverzeichnis
bind()
#include
<functional>
template<class F, class... BoundArgs> unspecified bind(F&& f, BoundArgs&&... parameterlist) template<class R, class F, class... BoundArgs> unspecified bind(F&& f, BoundArgs&&... parameterlist)
Kapselt einen Funktor, einen Funktions- oder Methodenzeiger
und koppelt die Argumente des Aufrufs an Kopien der Parameter,
Referenzwrapper (cref(variable) bzw. ref(variable)) oder
Platzhalter _1
, _2
, …, _N
aus dem Namensraum std::place_holders
.
Parameter
f | Funktion, Funktor |
parameterlist | Argumentliste des Funktors f |
Ergebnis
Rückgabewert: Funktor.
Siehe auch
Beispiel
- bind.cpp
#include <algorithm> #include <functional> #include <iostream> #include <string> #include <vector> int main() { // Binder mit Lambda-Ausdruck auto bruch = [](int x, int y) { return double(x)/y; }; auto reziprok = std::bind(bruch, std::placeholders::_2, std::placeholders::_1); // Umordnen von Parametern auto stammbruch = std::bind(bruch, 1, std::placeholders::_1); // Currying std::cout << bruch(3, 4) << " = 1/" << reziprok(3, 4) << '\n'; std::cout << "1/3 = " << stammbruch(3) << '\n'; std::cout << "1/3 = " << stammbruch(3, 5, 7, 9) << '\n'; // erlaubt, aber irreführend! /// @see http://stackoverflow.com/questions/13251976/why-do-objects-returned-from-bind-ignore-extra-arguments /// @see http://stackoverflow.com/questions/7394367/is-using-boostbind-to-pass-more-arguments-than-expected-safe // Binder mit Methodenzeigern auto add_space = std::bind(&std::string::push_back, std::placeholders::_1, ' '); std::vector<std::string> morse = { ".-", "-...", "-.-.", "-..", "." }; std::for_each(begin(morse), end(morse), add_space); // for (auto& s : morse) add_space(s); for (auto s : morse) std::cout << s; std::cout << '\n'; }
kennen/lib/bind.txt · Zuletzt geändert: 2019-11-20 15:04 von 127.0.0.1