fork(84) download
  1. #include <iostream>
  2.  
  3. void overloaded( int const &arg ) { std::cout << "by lvalue\n"; }
  4. void overloaded( int && arg ) { std::cout << "by rvalue\n"; }
  5.  
  6. template< typename t >
  7. void forwarding( t && arg ) {
  8. std::cout << "via std::forward: ";
  9. overloaded( std::forward< t >( arg ) );
  10. std::cout << "via std::move: ";
  11. overloaded( std::move( arg ) );
  12. std::cout << "by simple passing: ";
  13. overloaded( arg );
  14. }
  15.  
  16. int main() {
  17. std::cout << "initial caller passes rvalue:\n";
  18. forwarding( 5 );
  19. std::cout << "initial caller passes lvalue:\n";
  20. int x = 5;
  21. forwarding( x );
  22. }
  23.  
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
initial caller passes rvalue:
via std::forward: by rvalue
via std::move: by rvalue
by simple passing: by lvalue
initial caller passes lvalue:
via std::forward: by lvalue
via std::move: by rvalue
by simple passing: by lvalue