I’m implementing my own version of standard-compliant containers. I read the following. It states that elements should be destroyed using the allocator destroy. Does it suggest a container must call the destructor of elements, even if it is trivial? My interpretation is we should first have the need to call the destructor. If so, we must use the allocator destroy. Otherwise, we don’t need to call allocator destroy either. Under this interpretation, we can skip calling allocator destroy for elements with a trivial destructor. This can lead to optimization in sequence containers.
From cpp20 standard:
For the components affected by this subclause that declare an
allocator_type
, objects stored in these components shall be
constructed using the function
allocator_traits<allocator_type>::rebind_traits<U>::construct
and
destroyed using the function
allocator_traits<allocator_type>::rebind_traits<U>::destroy
(20.10.9.2), whereU
is eitherallocator_type::value_type
or an
internal type used by the container.
19
Based on the discussion, there is no clear answer to whether skipping allocator destroy for trivially destructible types is allowed by the standard. However people tend to prefer the implementation of calling it regardless and let the compiler take care of potential optimization. I will mark this question as closed.
0