Skip to content

Instantly share code, notes, and snippets.

@y-fedorov
Created March 23, 2019 11:04
Show Gist options
  • Save y-fedorov/3f6be6e1c12d4ebe4aa673e3f5eba642 to your computer and use it in GitHub Desktop.
Save y-fedorov/3f6be6e1c12d4ebe4aa673e3f5eba642 to your computer and use it in GitHub Desktop.
std::function example
#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