Are STL containers allowed to skip the allocator :: construct and allocator :: destroy calls if the object is trivially constructive / destroyed?

The question is in the title. Is this allowed for containers or can it be guaranteed that dispenser methods will be called even when the object is trivially constructive / destroyed?

I tried to find it, but came back empty-handed ... but if it's a duplicate, please let me know.

+6
source share
1 answer

Β§ 23.2.1 [container.requirements.general] / p3:

For components affected by this subclause that declare allocator_type , the objects stored in these components must be built using the allocator_traits<allocator_type>::construct functions and destroyed using allocator_traits<allocator_type>::destroy (20.7.8.2).

There are no provisions to exclude these calls outside the as-if rule. In fact, I cannot find a single instance of the word "trivial" in section 23, which lists standard libraries.

As to why they indicated type traits such as is_trivially_destructible , you will have to dig out the original proposal paper for justification. It is not used in the C ++ 14 standard, but is currently used to specify std::optional in the TS Library Basics project:

 ~optional(); 
  • Effects: If is_trivially_destructible<T>::value != true and *this contains a value, calls val->T::~T() .

  • Notes: If is_trivially_destructible<T>::value == true , then this destructor must be a trivial destructor.

+7
source

Source: https://habr.com/ru/post/973342/


All Articles