C++ Object Lifetime
kdab.com
kdab.com
RAII is one of the great things about C++, precisely because it allows detailed control over object lifetimes (even in the face of exceptions, etc).
However, I did learn something from this article: I didn't realize that a delegating constructor counted as "fully initializing" the object. The main constructor is, I imagine, just treated as some "extra code that is run after the real ctor"?
But I wouldn't rely on subtleties like that, or rely on others to know about it. Just use simple RAII (:
This does not appear to be the case. The code in question is not calling the constructor that throws! It's still calling
int main(){S s;}
If you change it to: int main(){S s(42);}
It will throw and not call the destructor (both latest clang and gcc).See for example https://godbolt.org/z/ohjchhGvz
So... seems like a bad example in the article (:
Right, mine too. It wasn't until I saw your comment that I thought to go and verify how it worked.
Only the latter 'int main(){S s(42);}' throws, and it will never call the destructor.
Or maybe I've misunderstood? Perhaps modify the code example I linked to on godbolt to show what you mean?
If an exception is properly handled (i.e. inside a try-catch block), the destructor of any object that might be destroyed after leaving said scope, is called as part of the stack-unwind, guaranteed by the specification.
yes it would be, because it "is not a function type, not a reference type, and not a void type" - it is a type named S.
this article is not very good - the lifetime of a named object in C++ begins when the object is defined, and ends when the name goes out of scope. pretty simple.
edit: and i should have added "and its constructor (if it has one) has completed"
What is the lifetime of the object to which p points?
The trouble with defining the reference-containing struct as a value type is that assignment to the struct assigns _through_ the reference rather than making it refer to something else.
My impression of the article is that more precise and pedantic language is required to talk about these things.
> the lifetime of a named object
in this case "new T" does not create a named object.
the lifetime of an unnamed object is determined explicitly by the programmer (i.e. "delete p" in this case), or implicitly if the object is being pointed to by something like a smart pointer, or is undefined (if you didn't assign the result of calling "new" to anything).
Reference data members are still tricky, though.
if you need a reference-like member, use a pointer.
So IMHO, the problem is such features consumption approach, if one is using raw pointers for any reason, all that area and subsequent should use classical structures, and use some kind of interfaces for to connect them to contemporary structures (suboptimal thought?), never mixing, case contrary to debug those things will be epic poetry (oh, the code uses auto, but is taking care of the pointers or no, damn, I have to read all the library for to know).
As a C++ novice that is practicing to improve this is the number one problem with finding tutorials. It's really hard for me to figure out which features are current best practice and which shouldn't be used together. I can obviously dig in to specific topics to read the history and pros/cons of features, but that's a very slow way to learn.