template <class _T1>
inline void constructInPlace(_T1 *_Ptr)
{
new (static_cast<void*>(_Ptr)) _T1();
}
I have known the place new about c++, I can't understand the above syntax!
template <class _T1>
inline void constructInPlace(_T1 *_Ptr)
{
new (static_cast<void*>(_Ptr)) _T1();
}
I have known the place new about c++, I can't understand the above syntax!
This syntax is known as placement new. It lets you construct objects in memory locations that you already own. It does NOT allocate memory for you.
In this case, a T1
object is being constructed in the memory location pointed to by _Ptr, since new
expects void*
, it is being cast down. The cast would happen implicitly anyway, looks like the explicit cast is to make the intent clear.