Advertisement
avr39-ripe

template function instancing

Apr 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T> int sortUp(T a, T b)
  8. {
  9.     return a > b;
  10. }
  11.  
  12. template <typename T> int sortDown(T a, T b)
  13. {
  14.     return a < b;
  15. }
  16.  
  17.  
  18.  
  19. int main()
  20. {
  21.     std::function<int(int, int)> fPtr[2] = { sortUp<int>, sortDown<int> };
  22.     std::cout << fPtr[0](1, 2) << std::endl;
  23.     std::cout << fPtr[1](1, 2) << std::endl;
  24.  
  25.     std::function<int(char, char)> fPtrC[2] = { sortUp<char>, sortDown<char> };
  26.     std::cout << fPtrC[0]('a', 'z') << std::endl;
  27.     std::cout << fPtrC[1]('a', 'z') << std::endl;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement