yo/classtemplates/examples/forward2.cc
raw| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | template<typename Fun, typename ArgType> |
| 5 | void caller(Fun fun, ArgType &&arg) |
| 6 | { |
| 7 | fun(std::forward<ArgType>(arg)); |
| 8 | } |
| 9 | |
| 10 | void display(ostream &out) |
| 11 | { |
| 12 | out << "Hello fm display\n"; |
| 13 | } |
| 14 | |
| 15 | void increment(int &x) |
| 16 | { |
| 17 | ++x; |
| 18 | } |
| 19 | |
| 20 | int main() |
| 21 | { |
| 22 | int x = 0; |
| 23 | caller(display, cout); |
| 24 | caller(increment, x); |
| 25 | cout << "x is now: " << x << '\n'; |
| 26 | } |