Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 0.88 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Derived assignment operator and perfect forwarding
  2. include <iostream>
  3.  
  4. struct foo
  5. {
  6.       foo &operator=(const foo &)
  7.       {
  8.               std::cout << "base copy assignmentn";
  9.               return *this;
  10.       }
  11.       foo &operator=(foo &&)
  12.       {
  13.               std::cout << "base move assignmentn";
  14.               return *this;
  15.       }
  16. };
  17.  
  18. struct bar: foo
  19. {
  20.       template <typename T>
  21.       bar &operator=(T &&x)
  22.       {
  23.               std::cout << "derived generic assignmentn";
  24.               foo::operator=(std::forward<T>(x));
  25.               return *this;
  26.       }
  27. };
  28.  
  29. int main()
  30. {
  31.     bar b, c;
  32.     b = c;
  33.     b = bar{};
  34. }
  35.        
  36. derived generic assignment
  37. base copy assignment
  38. base move assignment
  39.        
  40. derived generic assignment
  41. base copy assignment
  42. derived generic assignment
  43. base move assignment
  44.        
  45. operator=(X&&);
  46. operator=(const X&&);
  47. operator=(volatile X&&);
  48. operator=(const volatile X&&);