Tutorial by Examples

#include <iostream> #include <functional> std::function<void(int , const std::string&)> myFuncObj; void theFunc(int i, const std::string& s) { std::cout << s << ": " << i << std::endl; } int main(int argc, char *argv[]) { m...
Think about a situation where we need to callback a function with arguments. std::function used with std::bind gives a very powerful design construct as shown below. class A { public: std::function<void(int, const std::string&)> m_CbFunc = nullptr; void foo() { ...
#include <iostream> #include <functional> using std::placeholders::_1; // to be used in std::bind example int stdf_foobar (int x, std::function<int(int)> moo) { return x + moo(x); // std::function moo called } int foo (int x) { return 2+x; } int foo_2 (int x, in...
std::function can cause significant overhead. Because std::function has [value semantics][1], it must copy or move the given callable into itself. But since it can take callables of an arbitrary type, it will frequently have to allocate memory dynamically to do this. Some function implementations h...
/* * This example show some ways of using std::function to call * a) C-like function * b) class-member function * c) operator() * d) lambda function * * Function call can be made: * a) with right arguments * b) argumens with different order, types and count */ #include &lt...
Some programs need so store arguments for future calling of some function. This example shows how to call any function with arguments stored in std::tuple #include <iostream> #include <functional> #include <tuple> #include <iostream> // simple function to be called d...

Page 1 of 1