Restore stack object

Well, I know that sounds pure evil. I read this SO post and came across a technique for restoring a stack object. Main idea:

{
    T obj(...);  // dtor will be called at end of scope
    obj.~T();    // YOLO

    new (&obj) T(...);
    // obj goes out of scope. The compiler inserts `obj.~T();` here.
}

... so that we can use the same piece of memory as many times as we like. Is this code legal by standard? This is crazy, how is this behavior undefined?

+4
source share
1 answer

. . . ++ . - . "" , , UB per se. , , UB . , , .

+2

Source: https://habr.com/ru/post/1666768/


All Articles