Reputation: 2060
The code below compiles, but I am not 100% sure that it conforms to the standard:
#include <memory>
class A
{
public:
int x;
};
int main()
{
uint8_t m_storage[sizeof(A)];
new (m_storage) A();
auto deleter = [](A * p) { p->~A(); };
std::unique_ptr<A, decltype(deleter)> p(reinterpret_cast<A *>(m_storage), deleter);
p->x = 0;
return p->x;
}
Is it a proper usage of reinterpret_cast
?
Upvotes: 0
Views: 234
Reputation: 13719
Yes it is correct reinterpret cast.
Albeit not correctly aligned storage. And potentially wrong placement new (should cast parameter to void*
).
Upvotes: 1