When I am reading some article i found this line is so cryptic.
new (new_ptr + i) T(std::move(old_ptr[i]));
Can somebody explains how this syntax works?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Its a placement.
Here we can simplify this to:
This is normall C++03 and basically allows you to create an object dynamically in an area of memory that you have pre-allocated (its an advanced technique that most people will not use (unless they are building their own container like objects)).
The std::move() part is from C++11 and is creating a special reference type that allowes the move constructor to be used in the type T.
This basically says create a new T using the source object and use the move constructor for effeciency. This means the ‘source Obj’ will be left in an undefined state (thus not usable) after the move but it allows from efficient object creation.
Combining the two you get placement new with move construction using an element from an array as the source object.