Created
March 23, 2019 11:04
-
-
Save y-fedorov/3f6be6e1c12d4ebe4aa673e3f5eba642 to your computer and use it in GitHub Desktop.
std::function example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// Example from Jason Turner's video. | |
// https://youtu.be/JtUZmkvroKg?t=290 | |
template <typename T> | |
void print(T i, const std::string_view s) { | |
std::cout << i << ' ' << s << '\n'; | |
} | |
int main() { | |
// re-order function parameters | |
const auto myFunc = std::bind(&print<int>, std::placeholders::_2, std::placeholders::_1); | |
myFunc("Hello", 10); | |
myFunc("Hi", 42); | |
std::function<void (const std::string_view, int)> f2(myFunc); | |
f2("Test string", 15); | |
std::function<void (int, const std::string_view)> f3 = print<int>; | |
f3(45, "Test string"); | |
return 0; | |
} | |
/* | |
output: | |
10 Hello | |
42 Hi | |
15 Test string | |
45 Test string | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment