-
Notifications
You must be signed in to change notification settings - Fork 9
P1684: Consider adding a method to move the container out of the mdarray #22
Comments
A "move_container" function would not in general be possible, for example an all static extent |
I think it's worth pointing out that you'd still have the same issue with move construction. I do not have a good solution to that issue beyond extending compile time extent types with a null-like value, which I'm not actually suggesting. The only way to never have to worry about having a container size of zero is to make it so move construction is equivalent to copy construction, but I think that is an issue severable (hah) from the The issue can be dealt with separately because I'm suggesting composing move construction (inside of Additionally, the default contract for moved from objects is to not mess with them, other than setting it to a well defined state (eg. The last point I want to make: the idea I suggested to Mark is meant to be more all-in than |
@AnabelSMRuggiero makes a good point that the move constructor Being able to move the container out of the mdarray could be useful, but it was just a suggestion, not a poll result. |
A state where one can only assign-to/destroy is not the same as a valid but unspecified state. In a valid but unspecified state, one can call any function that doesn't have a precondition, as well as calling any function that has a precondition as long as the precondition has been satisfied. For instance, can you call We can, of course, explicitly add preconditions everywhere... The question is: how important are the move operations? |
The motivation for the poll to have the default container be |
@nliber wrote:
Excellent point -- thank you : - )
We definitely need to say what operations are allowed in the moved-from state. Are there any container requirements that specify what operations the container can support after move? I couldn't find any. If there are none, then we shouldn't allow users to do anything with a moved-from |
Do we agree with: The moved-from state has to maintain the class invariants (otherwise they are not invariants). If we say the only ops allowed are assign-to and destroy, then the invariants are weak and we have to add preconditions to all the other operations. There is the related issue on what state More generally, if the internal container is put into valid but unspecified state (moved-from, mutating operation threw an exception, etc.), I don't believe that is sufficient to assert that (Right now |
Valid but unspecified means you can call any function w/o a precondition. In practice, for containers the ones that are usually called are destruction, copy/move construction, assign-to, assign-from and
|
Another question about the moved-from state: if it is different than all the other valid states, will it be possible to test if it is in that state? |
I'm actually surprised As for The nuclear option if we can't trust move assignment from a container with an equal allocator is to construct the temporary in the same way, and then destroy the |
But it isn’t just To me, an exception being thrown during move is a very rare case, so other than making sure that our invariants are maintained, we shouldn’t be optimizing that at the expense of pessimizing the non-throwing case. Put another way, I’m only concerned about maintaining the basic exception safety guarantee, and not concerned with further reducing the likelihood of move throwing if it pessimizes the non-throwing case. So, can we do that? If If
I don't think that there are any other repercussions in doing the above, are there? |
This comes from 1684R2 LEWG review on 2022/04/19.
One reviewer suggested adding a method to move the container out of an
mdarray
. This would permit use cases like reclaiming and reusing an allocation for a sequence ofmdarray
.Another reviewer pointed out that this might leave the container in an invalid state, since the extents and mapping would no longer correctly describe the amount of available storage. This would violate the policy that a moved-from object be left in a valid but possibly unspecified state.
@AnabelSMRuggiero has some ideas for an interface that would "sever" the
mdarray
from its storage. It would return both the container, and anmdspan
viewing the container's storage. The result would ensure that themdarray
was in a safely usable moved-from state. For example, it could move the layout to the resultingmdspan
, and assign zero to all dynamic extents. The only issue would be if themdarray
has all static extents, but the container has a move behavior more likevector
thanarray
. This would make it impossible for the "severed"mdarray
to have a state usable for anything other than destruction or copy assignment. That's probably OK, but we would need to think about whether we would wantmdarray
ever to be in that state.The text was updated successfully, but these errors were encountered: