-2

After asking C++ Error linking in consumer file caused by static data field, I tried two different declarations for an instance of StateConservator:

StateConservator cs(*pContainer, pDoc->GetConfiguration());

and

StateConservator(*pContainer, pDoc->GetConfiguration());

The first did what I want, it only passes on the destructor only after the end of the scope. The second passes on the destructor in the own line of the declaration itself.

Is the compiler behaving correctly? If it is the correct behavior what is the way to declare an anonymous variable in that line?

Community
  • 1
  • 1
sergiol
  • 4,122
  • 4
  • 47
  • 81
  • 2
    Huh? You created a temporary; yes, it is destroyed immediately. What on earth do you expect to be able to do with a temporary over the long-term, when you can't actually refer to it by name? – Lightness Races in Orbit Mar 20 '15 at 11:12
  • I don't know why on earth you are downvoting a question that will clarify a doubt that many people have. – sergiol Mar 20 '15 at 11:15
  • 3
    Nobody else has ever had this "doubt". – Lightness Races in Orbit Mar 20 '15 at 11:18
  • @sergiol this is well documented behavior – Mgetz Mar 20 '15 at 11:18
  • 2
    It just doesn't make sense, @sergiol. You never come out and actually tell us what you're trying to do, but it _looks_ like you want to find some way to turn off the automatic destruction of a temporary (which you can achieve by binding it to a reference, incidentally — but then surely just declare it normally in the first place instead). But why would you do that? It's silly. It's nonsensical. It's _certainly_ not something that "many people" think about; quite the opposite. Tell us your _actual_ goal and maybe we can do something for you. – Lightness Races in Orbit Mar 20 '15 at 11:19
  • [What is an Anonymous Object?](https://stackoverflow.com/q/5330287/608639) and [Why do un-named C++ objects destruct before the scope block ends?](https://stackoverflow.com/q/2298781/608639) – jww Mar 04 '18 at 01:32

1 Answers1

9

You cannot have "unnamed" objects in C++. In the second case, the object is created and destroyed instantaneously because it is not associated with a name. The association with a name (e.g., variable) gives an object scope which controls its lifetime. By not naming an object, it's lifetime is bound to the statement. If it is given a name, then it is bound to the scope that the name is declared in.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
  • 1
    Well, with the new operator we can have unnamed objects... or not? – Paolo M Mar 20 '15 at 11:14
  • 1
    The `new` operator creates what I would call unbound objects. Dynamic allocation simply changes the scope. If you do not attach the result to a name, you leak memory. The scope that controls the lifetime is the allocator -- in most cases, the process level memory pool. – D.Shawley Mar 20 '15 at 11:15
  • using the new operator for instantiating the StateConservator class would destroy entirely its only purpose — to go back to the previous state implicitly. @PaoloM – sergiol Mar 20 '15 at 12:07
  • 1
    @sergiol _"using the new operator for instantiating StateConservator class"_ ... is certainly a **bad** idea! – πάντα ῥεῖ Mar 20 '15 at 12:09
  • What about `std::lock_guard(mutex);`? Because in this case, the name of the variable is irrelevant. – jaques-sam Oct 28 '19 at 10:17
  • @DrumM - I believe that the object will be created and destroyed immediately. IOW, you declare an object, construct it, and destroy it on the same line. The name is irrelevant but the lifetime of the object is certainly not what you intend since you will only own the lock for the execution of the statement. It will not remained locked for other statements because it is destroyed immediately. – D.Shawley Oct 28 '19 at 12:05
  • Yeah, correct. I found it in another topic. Thanks! – jaques-sam Oct 28 '19 at 13:34