10

Move semantics replace copy semantics in situations where copying is inefficient. Copy semantics deals fully with copyable objects, including const objects.

Already, there exists a myriad of non-copyable objects in c++11, for example std::unique_ptr. These objects rely on move semantics completely because moving from an object allows for invalidating it. This is important (imho) for popular design patterns like RAII.

A problem occurs when a const non-copyable object is assigned to an area of memory. Such an object can't be recovered in any way.

This is obviously important during the lifetime of the object, because of its constness. At the end of it's lifetime, when the destructor is called however, the (non-existent) object is briefly non-const.

I suggest that a moving destructor could be a valuable addition to the move semantics model.

Consider a simple situation where an unique_ptr is used in an unordered_set. You can insert into this set using a move constructor (or construct "emplace"), however if you wanted to move this pointer to another unordered_set (i.e. keeping it const) it would be impossible.

Essential, there is a iterator insert((possibly const) key&&) but no const key&& erase(iterator). In fact it would be impossible. The container could only be extended to return some pointer to the key, and forget about it.

A moving destructor could solve this ie const MyClass&& ~MyClass(), since it would only violate const during destruction (when the compiler considers the object is invalid anyway).

EDIT: I should point outconst MyClass&& ~MyClass() const actually makes more sense. The destructor doesn't have to modify anyhting, only destroy the object as if it were no longer a valid handle to whatever resource it controlled.

10
  • Const objects are also non const during construction.
    – woolstar
    Jan 13, 2014 at 7:21
  • A more appropriate forum for this would be one of the first 2 on this page: isocpp.org/forums Jan 13, 2014 at 7:21
  • Essentially it would copy only what it needs to (this is what move means in c++11) into a new object, and then destroys the old one, just like a normal move, but occuring but for a non-temp Jan 13, 2014 at 7:21
  • @BenjaminLindley true, i guess i don't want discussion so much as 'not possible' or 'similar to this proposal' Jan 13, 2014 at 7:23
  • @BenjaminLindley i posted the question elsewhere, time will tell if someone here wants to delete it, or answer it (or, you know, upvote it..) Jan 13, 2014 at 8:00

3 Answers 3

11

Imho you have identified a real need.

Your solution sounds a lot like what I have called destructive move semantics. This possibility is described in the original move semantics proposal. I think such a design is possible, though it is not without its problems. As far as I know, no one is working this area on the standards committee.

There are simpler ways to extract move-only types out of the associative containers that would not require language changes (aside maybe from allowing type-punning without undefined behavior).

N3645 is a library-only proposal which puts a nested node_ptr type into each container. The node_ptr is a lot like a unique_ptr. It has unique ownership of a node in an associative container. But when you dereference it, you get non-const access to the value_type in the node, instead of the node itself. extract and insert members are added to the associative containers allowing one to insert and remove nodes (owned by the node_ptr) to/from the containers.

You could use this to remove a node from a container, and then move a move-only type out of the node, and let ~node_ptr() clean up the node when you are done with it. The paper includes this example to demonstrate this functionality:

set<move_only_type> s;
s.emplace(...);
move_only_type mot = move(*s.extract(s.begin())); // extract, move, deallocate node

Note that s.extract is noexcept, as is ~node_ptr() of course. If the move construction of move_only_type is noexcept, then this whole operation is noexcept. Otherwise if the move construction throws, the set is left as if the item had been erased from the set.

At the moment, no progress is being made on N3645. It has not been voted into a working draft, and I have no confidence that it ever will be.

Update C++17

I stand corrected: The functionality I describe above was voted into C++17 with P0083R3. Thanks to Cosme for reminding me of this in the comments below.

2
  • thanks, this is the kind of answer i was looking for. i understand the library could be changed to solve this particular example, but i hope people reading this realise it was just a particular example - yes "destructive move semantics" is exactly the problem i was trying to discuss Jan 14, 2014 at 0:31
  • The last revision of the proposal mentioned above: P0083R3
    – Cosme
    Sep 9, 2017 at 17:26
2

Sorry, but the premise is flawed.

An unordered_set doesn't actually hold const objects. It's just not giving you write access to the contained elements. That's a property of the accessors only.

It would be possible to add an key erase(iterator) function which just moves the element out to a temporary. I'm not sure why you'd want a key&& there.

As for const MyClass&& ~MyClass() const, that doesn't make sense for three reasons: dtors have neither return types nor CV classification, nor is overload resolution done for them.

10
  • of course it doens't make sense as it stands, that's not the question. "It would be possible to add an key erase(iterator) function which just moves the element out to a temporary." No. You can't move from a const, except with a const move operator. Such an operator is equivalent to a const copy operator. The problem is it can be invoked anywhere in code. Typically the non-copyable class is used to signal a unique object - how would "moving a temporary" work? what happens when the original, moved from object gets destroyed? It can't be modified so it frees whatever resource it was holding. Jan 13, 2014 at 10:42
  • Basically, the only solution is to allow access to a handle to the memory (with its own pros and cons, and very un-generic, and fails to address the problem of where an object is deleted (see here for someone else thoughts on a similar topic.)) or to provide some primitive that allows constructing from an object at the end of said objects lifetime (it is the only way to prevent double deleting, as would be caused by moving) and my suggestion one such possible primitive (only an example, i suspect there are better ways) Jan 13, 2014 at 10:46
  • and also the unordered_set is just an example, (it can have const objects) and moving from an object would invalidate the hash (so effectively it must have const keys from an external perspective) Jan 13, 2014 at 10:48
  • 1
    @user3125280: The set itself isn't const (obviously, since erase isn't const), nor are its members (You keep claiming they are, please provide actual proof. Ignore the accessors). And yet that's the only reason you give for such an important addition. Sorry, that's not enough justification in my book.
    – MSalters
    Jan 13, 2014 at 10:52
  • "the set itself isn't const" true (though you can delete through constant pointers, erase through const_iterator, etc) - consider moving from one key. The move creates and returns a new key. The old key is now modified. Critically, the previous key may have a new hash. The container doesn't know this, and has lost control of the element. Currently, we can't remove this except with a const copy constructor, agreed? But then the unchanged element is still deleted through its old destructor (which assumes it owns memory or something) Jan 13, 2014 at 10:56
1

So basically you're saying it should be possible to move a const object into another const object and destroy the original?

Sorry but I think the whole point of making it const is to prevent this.

Otherwise it would form a loophole: you could destroy-move a const object out of its memory location, then you destroy-move another const object into the memory location of the first one (via placement new).

Now the object has changed even though it was const... so essentially const was useless.

See comments below...

21
  • yes, (calling different then normal destructor so the normal one doesn't get called twice). Actually that's not really a loophole, in the sense that you can already do that in c++. it is undefined behaviour and breaks some rules. I would have to look around more to give a definitive response, but I think that deleting the object means that it is no longer valid to access an object at that address (You would have to use hacks to keep that memory anyway while destroying the object there). Even if that object was const qualified. It is simply undefined. Let me make an ideone example.. Jan 15, 2014 at 11:00
  • I tried but it doesn't seem to have tricked the compiler (since it is illegal). I'm sure you know that people new to c++ often make the mistake of returning a reference to a temporary. This is the same situation. Once the object is out of scope or deleted by a pointer (or by a hypothetical destructor) it is invalid to access it in anyway. Const doesn't make an object permanent, only read only during it's lifetime. Jan 15, 2014 at 11:15
  • @user3125280: Hmm, I'm pretty sure there's legal ways to do this, but interestingly I think the problem is even there right now and might have nothing to do with your proposal. For example struct S { char x; }; char buf[sizeof(S)]; S const *p = new (buf) const S(); foo(p); S moved(p->~S()); new (buf) const S(moved.~S()); Whoops, we just modified the target of p even though it was constructed as a const object... maybe your proposal doesn't affect it after all?
    – user541686
    Jan 15, 2014 at 11:20
  • @Mehrad that's what i meant - that code is undefined, the origianl const S has been destroyed. Trying to access it by P is wrong, even though it aliases with a new const S. This is programming error, and not a c++ error. Access to a destroyed object is always undefined. Jan 15, 2014 at 11:48
  • @user3125280: Is it really undefined? This is POD, the destructor and constructor are trivial, so there is no notion of destruction or construction here. I'm pretty sure it's legal, isn't it?
    – user541686
    Jan 15, 2014 at 12:00

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.