fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct S {
  5. std::string name_ = "defaulted";
  6. S()
  7. {
  8. std::cout << "ctor\n";
  9. }
  10. S(const char* name) : name_(name) {
  11. std::cout << "char* ctor \n";
  12. }
  13. S(const S& copy)
  14. {
  15. std::cout << "copy ctor\n";
  16. name_ = copy.name_;
  17. }
  18. S(S&& rhs) {
  19. std::cout << "move ctor " << name_ << "\n";
  20. name_ = rhs.name_;
  21. rhs.name_ = "new";
  22. name_ += " moved"; }
  23. };
  24.  
  25. void fn(S s)
  26. {
  27. std::cout << "fn(" << s.name_ << ")\n";
  28. }
  29.  
  30. template<typename T>
  31. void fwd_test(T&& t)
  32. {
  33. fn(std::forward<T>(t));
  34. fn(std::forward<T>(t));
  35. }
  36.  
  37. int main() {
  38. fwd_test(S("source"));
  39. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
char* ctor 
move ctor defaulted
fn(source moved)
move ctor defaulted
fn(new moved)