0

My other question topic (for reference. not needed to answer this question. more so for other readers who do not know why Output Line 6 is being invoked): Passing by reference - why is this destructor being called?

Please help me (and others) better understand when copy constructors are invoked by examining the code (and my comments that show my understanding) below.

I am using Visual Studio 2015 as a reference to my compiler.

I do not think I have a way of confirming myself as to whether or not my reasoning for why these constructors are called is correct which is why I am asking here on StackOverflow for your input.

I question lines 7-18 specifically ( I feel fairly confident in my reasoning for lines 1-6 but feel free to correct me if I'm wrong ).

To be explicit: If my reasoning as to why these constructors destructors are appearing for line 7-18 is incorrect, then what is the correct reason as to why these constructors/destructors are being invoked?

struct X:

struct X { // simple test class
   int val;

   void out(const std::string& s, int nv)
   {
      std::cerr << this << "–>" << s << ": " << val << " (" << nv << ")\n";
   }

   X() { 
      out("X()", 0); 
      val = 0; 
   } // default constructor

   X(int v) { 
      val = v; 
      out("X(int)", v); 
   }

   X(const X& x) {
      val = x.val; 
      out("X(X&) ", x.val); 
   } // copy constructor

   X& operator=(const X& a) // copy assignment
   {
      out("X::operator=()", a.val); 
      val = a.val; 
      return *this;
   }
   ~X() { 
      out("~X()", 0); 
   } // destructor
};

X glob(2); // a global variable

      // Output Line 1: X(int): 2 (2)
      // Reasoning: from X(int v) function

X copy(X a) { 
   return a; 
}

X copy2(X a) { 
   X aa = a; 
   return aa; 
}

main function:

int main()
{
   X loc{ 4 }; // local variable 

      // Output Line 2: X(int): 4 (4)
      // Reasoning: from X(int v) function

   X loc2{ loc }; // copy construction 

      // Output Line 3: X(X&) : 4 (4)
      // Reasoning: from X(const X& x) function

   loc = X{ 5 }; // copy assignment 

      // Output Line 4: X(int): 5 (5)
      // Reasoning: from X(int v) function

      // Output Line 5: X::operator=(): 4 (5)
      // Reasoning: from the '=' operator overload

      // Output Line 6: ~X(): 5 (0)
      // Reasoning: X { 5 } is not an object. gets terminated at ';'

   loc2 = copy(loc); // call by value and return 

      // Output Line 7: X(X&): 5 (5)
      // Reasoning: from making a copy of X { 5 } for loc

      // Output Line 8: X(X&): 5 (5)
      // Reasoning: from making a copy of "loc" in parameter

      // Output Line 9: ~X(): 5 (0)
      // Reasoning: after copy made in (7) leaves function

      // Output Line 10: X::operator=(): 4 (5)
      // Reasoning: from the '=' operator overload

      // Output Line 11: ~X(): 5 (0)
      // Reasoning: same reason as output (6) above

   loc2 = copy2(loc);

      // Output Line 12: X(X&): 5 (5)
      // Reasoning: from making a copy of X { 5 } for loc

      // Output Line 13: X(X&): 5 (5)
      // Reasoning: from making a copy of "loc" in parameter

      // Output Line 14: X(X&): 5 (5)
      // Reasoning: from creating "aa"

      // Output Line 15: ~X(): 5 (0)
      // Reasoning: "loc" copy from (13) destroyed (see line 6 for reason)

      // Output Line 16: ~X(): 5 (0)
      // Reasoning: X { 5 } copy from (12) destroyed (see line 6 for reason)

      // Output Line 17: X::operator=(): 5 (5)
      // Reasoning: from the '=' operator overload

      // Output Line 18: ~X(): 5 (0)
      // Reasoning: after "aa" copy from (14) leaves function 
.
.
.
}
Steve Cho
  • 431
  • 2
  • 5
  • 10
  • When you say `evoke`, do you actually mean `elide`? – SergeyA Oct 26 '18 at 14:50
  • 1
    related/dupe: https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization – NathanOliver Oct 26 '18 at 14:52
  • @SergeyA ah, I meant to say "invoke" – Steve Cho Oct 26 '18 at 14:53
  • @NathanOliver I was also mulling this as a dupe, but I am still not sure it's a clear cut duplicate. – SergeyA Oct 26 '18 at 14:55
  • Since there are so many topics about constructors/destructors/elision, I was hesitant to post this question but with that specific topic, its more so the definition of elision and whether or not it is performed by the compiler in certain cases. This question is more directed at the reasoning behind various examples of constructor/destructor appearances that are not "elided" by the compiler. – Steve Cho Oct 26 '18 at 15:02

0 Answers0