fork(2) download
  1. #include <cstdio>
  2.  
  3. template<typename T>
  4. class Test {
  5. private:
  6. T value_;
  7.  
  8. public:
  9. mutable bool has_copies;
  10.  
  11. Test(const T& value) : value_(value), has_copies(false) {}
  12.  
  13. template<typename Related> // remove template here to fix behavior
  14. Test(const Test<Related>& t) {
  15. printf("In copy constructor\n"); fflush(stdout);
  16. value_ = t.value_;
  17. has_copies = true;
  18. t.has_copies = true;
  19. }
  20. };
  21.  
  22. int main() {
  23. Test<int> t1(42);
  24. printf("Before constructor\n");
  25. Test<int> t2(t1);
  26. printf("After constructor:\n");
  27. printf(" t1 thinks it %s copies\n", t1.has_copies ? "has" : "doesn't have");
  28. printf(" t2 thinks it %s copies\n", t2.has_copies ? "has" : "doesn't have");
  29. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Before constructor
After constructor:
    t1 thinks it doesn't have copies
    t2 thinks it doesn't have copies