Is this normal with a strict alias rule?

struct Test {
    void doAction() {}
};

// Create and save into a void*
void *ptr = new Test;

// Real use through a Test*
Test *t = static_cast<Test *>(ptr);
t->doAction();

// Delete
delete static_cast<Test *>(ptr);

ptr is used only to save the address of the object, and the address is only dereferenced by the true type of the object.

So, if it is not dereferenced by an unrelated type, is it okay with the strict rule of aliases?

+4
source share
1 answer

, /. void*, (, , static_cast, void* , , , , ).

, . , t static_cast<T*> , , .

+5

All Articles