mardi 28 mars 2023

When should propagate_on_container_copy_assignment and propagate_on_container_move_assignment be true?

I am trying to understand allocators, but I do not understand exactly the uses of some standard aliases. In particular, I have already read in cppreference the consequences of the propagate_on_container_copy_assignment and propagate_on_container_move_assignment aliases on container's assignment operations:

propagate_on_container_copy_assignment

If (this member is derived from std::true_type and) the allocators of the source and the target containers do not compare equal, copy assignment has to deallocate the target's memory using the old allocator and then allocate it using the new allocator before copying the elements (and the allocator).

propagate_on_container_move_assignment

If this member is not provided or derived from std::false_type and the allocators of the source and the target containers do not compare equal, move assignment cannot take ownership of the source memory and must move-assign or move-construct the elements individually, resizing its own memory as needed.

Reading the notes below and checking some implementations of containers, it seems the cost of the assignment operation depends from the value of these aliases, if the source and the target allocators do not compare equal. Then, if an allocator provides fast allocate and deallocate operations but has the 'wrong' values for these aliases, the cost can be very high.

In general, under what conditions should the designer of a custom allocator mark one or the other alias as true or false?

NOTE: I am not asking what the designer or the container should do if these aliases are true or false, but in which conditions the designer of the allocator should declare them as true or false.

Aucun commentaire:

Enregistrer un commentaire