what is the meaning for &t for expression new (&t) T(t) in c++?

c++

Solution

This is referred to as the 'placement new' syntax. The `T` value is constructed in the address that is specified by `&t`.

This sample is a bit off since it's creating a new T in the exact location of an existing T using the copy constructor. I think it's easier to explain this concept with an explicit address. Here is a variation of this code.

T t;
void* pAddress = malloc(sizeof(T));
new (pAddress) T(t);

// Or just creating a T without a copy ctor
new (pAddress) T();

Problem

what is the meaning for &t for expression new (&t) T(t) in c++? as titled. ``` T t; new (&t) T(t); ```

Original source

Related problems