Ahoj, z cppreference som trosku upravil kod :
#include "pch.h"
#include <memory>
#include <new>
#include <iostream>
#include <iomanip>
struct Tracer {
int value;
~Tracer() { std::cout << value << " destructed\n"; }
};
int main()
{
alignas(Tracer) unsigned char buffer[sizeof(Tracer) * 8];
for (int i = 0; i < 8; ++i) {
new(buffer + sizeof(Tracer) * i) Tracer{ i }; //manually construct objects
std::cout << std::setw(9) << (long long) &buffer + sizeof(Tracer) * i << '\n';
}
//auto ptr = std::launder(reinterpret_cast<Tracer*>(buffer));
auto ptr = reinterpret_cast<Tracer*>(buffer);
std::destroy(ptr, ptr + 8);
}
A zaujima ma, k comu tam potrebujeme:
->alignas (Tracer) .. viem, ze sa jedna o zarovnanie v pamati, ale tam to asi konci...
-> std::launder , jediny priklad co som k tomu nasiel pokial sme na adrese stareho objektu (ktory mal nejaky const atribut) vytvorili novy a chceli sme ho menit, ale nerozumiem preco ho pouziva aj teraz.
Dakujem :)