Question
Why destructor needs to be accessible even when it is not called?
Having class X
, the following object initialization:
new (ptr) X(X());
requires an accessible destructor even since C++17. Why is that, when the object is initialized by the default constructor directly in the storage pointed to by ptr
, so no temporary should be involved?
Example code:
struct X {
X() { }
X(const X&) = delete;
X(X&&) = delete;
~X() = delete; // or, e.g, private
};
void test(void* ptr) {
new (ptr) X(X()); // error: attempt to use a deleted function
}
Demo: https://godbolt.org/z/Khac1z8r3
UPDATE
This is a similar question: Why is public destructor necessary for mandatory RVO in C++?. But my case is different, since in my code, the problem of potential destructor calls due to exceptions does not apply (or, at least, I can't see it there).