|
Boost : |
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2022-02-18 15:55:04
On 2/18/22 18:15, Andrea Bocci wrote:
> Hi Andrey,
> now I'm curious... is
>
> Â alignas(int) unsigned char storage[sizeof(int)];
> Â int* p1 = new (storage) int(1);
> Â int* p3 = new (storage) int(3);
> Â std::printf("%d\n", *p1);
>
> different from
>
> Â Â int* p1 = new int(1);
> Â Â int* p3 = new (p1) int(3);
> Â Â std::printf("%d\n", *p1);
>
> or even
>
> Â Â int p1 = 1;
> Â Â int* p3 = new (&p1) int(3);
> Â Â std::printf("%d\n", p1);
>
> ?
In the examples above, you're reusing storage of the object with a
different object of the same type, that is the old object is
"transparently replaceable" by the new one. See here:
https://en.cppreference.com/w/cpp/language/lifetime#Storage_reuse
In this case, p1 remains valid and can be used to access the new object.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk