Why does my MSVC12 compiler not like this?
#include <new>
class thing
{
public:
thing() {}
~thing() {}
static void operator delete(void* ptr) = delete;
};
int main()
{
int g;
void* p = &g;
thing* t1 = new(p) thing();
t1->~thing();
return 0;
}
The error I get is oddly on the closing brace of main():
Error 2 error C2280: 'void thing::operator delete(void *)' : attempting to reference a deleted function
If I comment out the explicit destructor call, the error goes away, implying that the explicit destructor call is trying to call operator delete(void*). This does not make intuitive sense. As you can probably see from the code here, I've already managed my own memory, and I don't want anyone to call delete on thing ever.
sizeof(thing) <= sizeof g
, thenew
may write out of bounds otherwise. Also, can you reproduce the bug usingvoid *p = ::operator new(sizeof(thing));
, instead of using anint
variable? – Shupeoperator delete()
(by acting as a wrapper for thedelete
and destructor calls). For some reason, it would appear that in this particular situation, placement new makes it try to use that method instead of using the destructor directly. I may be wrong, but that's the only logical connection I can think of betweenoperator delete()
and~T()
in MSVC, when the object isn't explicitlydelete
d. – Deutero