After reading this link , I've known that placement new
was too hard to use properly. Then I found std::allocator
, so I thought std::allocator
should have used placement new
because it could separate allocation and do construction in two steps.
However, it seems that How are allocator in C++ implemented? tells me that std::allocator
is implemented by operator new
, instead of placement new
. I'm confused now. If it doesn't use placement new
, how could it separate allocation and do construction in two steps?
You have to differ between the operator new
function and new
expressions .
The former (the operator new
function) only allocates memory. The latter ( new
expression) calls operator new
to allocate memory and then invokes the constructor.
std::allocator
have two functions to implement allocation and construction: allocate
and construct
.
The allocate
function uses the operator new
function to allocate memory. The construct
function uses placement-new to construct objects. Which seems to be the two-step process you want.
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.