Advertisement
mbazs

C++ forwarding

Dec 28th, 2018
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <utility>
  2. #include <iostream>
  3.  
  4. /* forwarded-to fn */
  5. void f1(int& a) {
  6.     std::cout << "forwarded to f1(int& a): " << a << std::endl;
  7. }
  8.  
  9. /* forwarded-to fn */
  10. void f1(int&& a) {
  11.     std::cout << "forwarded to f1(int&& a): " << a << std::endl;
  12. }
  13.  
  14. /* forwarding fn template */
  15. template<class T>
  16. void f0(T&& input) {
  17.     std::cout << "f0() forwarding " << input << std::endl;
  18.     f1(std::forward<T>(input));
  19. }
  20.  
  21. int main()
  22. {
  23.     std::cout << "=== A section ===" << std::endl;
  24.     int a = 55;
  25.     f0(5);
  26.     f0(a);
  27.  
  28.     std::cout << std::endl;
  29.     std::cout << "=== B section ===" << std::endl;
  30.     int b = 10;
  31.     auto&& b1 = b;
  32.     auto&& b2 = 11;
  33.     f0(b1);
  34.     f0(b2);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement