fork(11) download
  1. #include <iostream>
  2.  
  3. #include <functional>
  4. #include <type_traits>
  5.  
  6. class testClass
  7. {
  8. public:
  9. testClass() = default;
  10. testClass( const testClass & other ) { std::cout << "COPY C" << std::endl; }
  11. testClass & operator=(const testClass & other ) { std::cout << "COPY A" << std::endl; }
  12. };
  13.  
  14. template< class T>
  15. void testFunc(T && t)
  16. { [test = std::conditional_t<
  17. std::is_lvalue_reference<T>::value,
  18. std::reference_wrapper<std::remove_reference_t<T>>,
  19. T>{std::forward<T>(t)}]{}(); }
  20.  
  21. int main()
  22. {
  23. testClass x;
  24. std::cout << "PLEASE NO COPY" << std::endl;
  25. testFunc(x);
  26. std::cout << "DONE" << std::endl;
  27.  
  28. std::cout << "COPY HERE" << std::endl;
  29. testFunc(testClass());
  30. std::cout << "DONE" << std::endl;
  31. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
PLEASE NO COPY
DONE
COPY HERE
COPY C
DONE