Fun With C++ [Beginners Stay Away, Lest Ye Be Corrupted]

Started by
30 comments, last by SiCrane 14 years, 1 month ago
Placement new and explicit destructor calls naturally do have a place, but even boost::variant doesn't do it with itself.

Quote:
It allows a reasonable copy constructor/assignment operator to be defined for classes with const member variables (and reference member variables).


For const members you could use a const_cast (which should show how fishy the idea is conceptually) and for reseatable reference members you could use pointers.
Advertisement
What's the point of making a member const if you intend to modify it? That's like making something private and complain that it isn't public...
Quote:Original post by cache_hit
I haven't actually looked at the source code, but it sure sounds to me like they're doing this.

I have and they don't.
Quote:For const members you could use a const_cast (which should show how fishy the idea is conceptually) and for reseatable reference members you could use pointers.


Yes, you could - and you probably should. I was just suggesting why some people think this is a good idea.

Quote:I don't believe any good ever comes from using the object rebirth techinque.

But isn't it the technique that std::vector uses to seperate construction/destruction from allocation/dealloaction?

Object rebirth:
aVector.pop_back();
aVector.push_back(my_struct());
Quote:Original post by Nitage
But isn't it the technique that std::vector uses to seperate construction/destruction from allocation/dealloaction?

No, when you call pop_back() it invalidates all iterators, pointers and references to the last element. If you follow a pop_back() with a push_back() those are two different objects. And more importantly, std:vector doesn't destroy itself just to recreate itself in the same location.
Kind of a minor distinction IMO. if you agree it's ok and there are legitimate use cases for manually invoking destructors of owned objects, and you agree that it's ok to write 'delete this', indicating that an object owns itself, then i dont see why this is anything other than a natural extension. Granted, a natural extension that you likely will never need, but its no stranger to me than "delete this"
Quote:Original post by cache_hit
Kind of a minor distinction IMO.

What is?
Manual destruction / in-place construction of yourself as opposed to some arbitrary child you claim ownership of. Either way if ownership semantics are well defined, i think this is no stranger than delete this, albeit maybe a little more rare as far as use cases are concerned
Quote:
Kind of a minor distinction IMO. if you agree it's ok and there are legitimate use cases for manually invoking destructors of owned objects, and you agree that it's ok to write 'delete this', indicating that an object owns itself, then i dont see why this is anything other than a natural extension.


I suppose the difference is that in one case you'd be doing it with an automatic object and therefore you must at any cost succeed in replacing this. You can't avoid the destructor being called on the object.

When you do it with an owned object (as vector and variant do), those wouldn't be destroyed automatically when the owner goes out of scope. They were explicitly created and have to be explicitly destroyed (and you can easily omit the last step if you lost the object in the meantime).

delete this also wouldn't be done on an automatic object.

-------

Go ahead and use with objects whose construction cannot fail. But still there might be a better use case than just hiding const_cast, and reseating references.
Quote:Original post by visitor
Go ahead and use with objects whose construction cannot fail.


You can even enable such logic at compile-time by using meta-programming magic to detect if a class has a nothrow constructor, in which case performing the operation actually would support a stronger exception guarantee.

This topic is closed to new replies.

Advertisement