简体   繁体   中英

std::uninitialized_move with specific allocator

I'm writing a custom container template class that, as many (if not all) the containers in the stl, can use a specified allocator type. To implement the range insert function, I need to move some of the elements in the container a number of spaces forward, where the memory is still uninitialized. To do this I want to use some (nonexistent) version of std::uninitialized_move() that uses the allocator in the container.

The other option is to do the move-construction of the objects using the allocator in a for loop and destroy the constructed objects in case of an exception. That's basically re-implementing std::uninitialized_move() with an extra feature.

The standard library implementation for my compiler (GCC) has exactly the functions I need ( std::__uninitialized_move_a() , std::__uninitialized_copy_a() , etc), and are in fact used in the implementation of std containers, but I think those are rather compiler-specific.

Should I use this functions (portability)? Or is there other, more practical, option? Maybe there is something in the standard library that I'm missing.

Should I use this functions (portability)?

You shouldn't use the GCC internal functions.

No, there doesn't seem to be standard equivalents. You can write your own versions of those functions. If you do, note that CustomAlloc::construct is an optional function (for example, std::allocator doesn't have this function since C++20), so it should be use through std::allocator_traits<CustomAlloc>::construct . This has additional benefit of being constexpr since C++20.

Or is there other, more practical, option?

One option is to ignore the possibility that CustomAlloc::construct has been implemented to do something other than direct placement new, and thereby simply use the standard std::uninitialized_move .

This technically limits the allocators that your container supports in theory, but on the other hand I've never seen such custom allocator used in practice. This may be reasonable limitation if the container is for internal use at least. If you do this, document the behaviour carefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM