0

I found strange behavior and would like to understand how and why it works the way it does (tested on msvc 14.0, Visual Studio 2015).

Lets assume we have structure test_struct

   struct test_struct
   {
      test_struct() : _number(5) {}
      int _number;
      ~test_struct()
      {
         static int i = 0;
         std::cout << i++ << std::endl;
      }

      test_struct& get_me() { test_struct a; return a; }
   };

And we have following code:

   {
      test_struct();                         //(0) will be written 0, because object was created and died just at same moment, because it's scope is a point of it's creation.
      test_struct struct2;                   //(1) nothing will be written, because object is still in scope
      test_struct& ref1 = struct2.get_me();  //(2) trying to get reference to object that is out of scope, keeping reference to object do not save it. Will be written 1.
      test_struct& ref2 = test_struct();     //(3) Nothing will be written !!?? Despite fact, that object was created similar as at (0), so had to die immediately, and despite fact, that keeping reference to such object do not save it, as we learned from (2)
      {
         test_struct& ref3 = struct2;        //nothing will be written, because moving out of reference scope do not destruct object
      }
   }                                         //will be written 2 and 3, because now moving out of reference scope destructs object

Questions are:

  1. If existing reference can handle object existing, why line (2) leads to undefined behavior and losing object?
  2. If existing reference can't handle object, why there is no destruction at line (3), and it happens at moment when reference leaving scope?
  3. If object without name constructed inside any scope live until exit from scope, why at line (0) there were destruction of such object?
  4. If object without name constructed inside any scope DON'T live until exit from scope, and reference do not handle objects, why object constructed without name at line (3) was not destructed after created?

It looks like sometimes reference can handle object, sometimes not. Does there exists any clarification on that?

Arkady
  • 2,084
  • 3
  • 27
  • 48
  • 1
    Asking for the behavior of undefined behavior is pretty useless. – πάντα ῥεῖ Jul 10 '16 at 11:52
  • So at line (3) there is undefined behavior? Really that is root of my question, does that behavior normal or just undefined / wrong ? – Arkady Jul 10 '16 at 11:53
  • `test_struct a; return a;` with a reference return type is undefined behavior. – πάντα ῥεῖ Jul 10 '16 at 11:54
  • Yes - your compiler should tell you about it: http://ideone.com/wD0X2z – Oliver Charlesworth Jul 10 '16 at 11:54
  • πάντα ῥεῖ, that's true, but that's just example of another behavior on same compiler. Question is about line (3): Is line (3) undefined behavior and why it behaves as it does? – Arkady Jul 10 '16 at 11:56
  • 1
    Have a look at https://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/ – Peter Jul 10 '16 at 11:56
  • 1
    @Arkady _"why it behaves as it does?"_ Please read my 1st comment again. – πάντα ῥεῖ Jul 10 '16 at 11:57
  • @πάνταῥεῖ, your first comment have meaning just if line (3) is for sure undefined behavior, but I don't know. That is source of question: is it undefined, or it behaves as it should. – Arkady Jul 10 '16 at 11:58
  • @Arkady All of these statements have **undefined** behavior, as it says there's nothing predictable, and you shouldn't rely on any observations. – πάντα ῥεῖ Jul 10 '16 at 12:00
  • @OliverCharlesworth, msvc-2015 writes that just for `int()`, but not for user's types at compile time. I thought there is a exception for them, or something. Does there exists any document about that? – Arkady Jul 10 '16 at 12:00
  • @πάνταῥεῖ, line (2) is undefined behavior for sure, but which other lines are undefined, or behaves as they should -- is the root of question. Should reference handle object's life or not? Is that correct answer: always not, and if it does -- it's problem of compiler? – Arkady Jul 10 '16 at 12:02
  • and it has nothing related to http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope question – Arkady Jul 10 '16 at 12:05
  • @Arkady The code [doesn't compile](http://ideone.com/cZeBCe) using g++, and neither does it compile using [clang](http://rextester.com/NUNW99765). – PaulMcKenzie Jul 10 '16 at 12:14
  • @PaulMcKenzie, and if reference is `const` at line (3), it compiles... – Arkady Jul 10 '16 at 12:19
  • @Peter, thanks, that clarify everything – Arkady Jul 10 '16 at 12:20

0 Answers0