3 ms·
After the object is constructed by placement-new, the class methods still needs to reinterpret_cast the char array to an object pointer to access the object. I
by sillycross 5y ago
After the object is constructed by placement-new, the class methods still needs to reinterpret_cast the char array to an object pointer to access the object.
I don't think in this specific case there is an UB involved, but I'm not language standard lawyer so I'm not sure. I feel the standard's specification on what is allowed to reinterpret_cast and what isn't is arcane (or at least far from straightforward to understand).
- tylerhou 5y agoNot too terrible to create a private method that does the reinterpret_cast for you. I've had to use this technique in the past and therefore dived into the standard for quite a while. I don't recall encountering any UB concerns.
- int_19h 5y agoYou will get the properly typed pointer to object from new, so if you want to play it completely safe wrt UB, it can be stashed away alongside the array in the public class; this adds sizeof(T*) to the latter, but avoids casts entirely. But, yes, you do technically need std::launder to get it directly via the array.