§3.7.4.2/2 contains the following sentences:
The global
operator delete
with exactly two parameters, the second of which has typestd::size_t
, is a usual deallocation function. Similarly, the globaloperator delete[]
with exactly one parameter is a usual deallocation function. The global operatordelete[]
with exactly two parameters, the second of which has typestd::size_t
, is a usual deallocation function.37
37) This deallocation function precludes use of an allocation function void operator new(std::size_t, std::size_t)
as a
placement allocation function.
Not only did I not understand the reason for this footnote, but also I noticed that this placement form, alluded in the footnote, doesn't exist in §18.6.1.3 Placement Forms [new.delete.placement].
Edit In order to verify what @Sander De Dicker is saying in his answer, I tested the following snippet:
#include <iostream>
void* operator new (std::size_t count, int i1, int i2, int i3){
void *p = malloc(count);
if (!p) throw std::bad_alloc{};
std::cout << "operator new" << '\n';
return p;
}
void operator delete (void* p, int j1, int j2, int j3)
{
free(p);
std::cout << "operator delete" << '\n';
}
class A {
public:
A() { std::cout << "A()" << '\n'; };
~A() { std::cout << "~A()" << '\n'; }
};
int main()
{
try
{
A *p = new(0, 0, 0) A;
delete p;
}
catch (std::bad_alloc&) { exit(1); }
}
In all 3 compilers that I have available (VS2015, clang and g++) the code invoked the placement operator new(size_t, int, int, int)
, but didn't invoke the placement operator delete(void*, int, int, int)
, but the operator delete(void*)
. Now, I'm more confused than I was when I posted the question.
operator new(size_t, size_t)
because this requires a correspondingoperator delete(size_t, size_t)
for deletions that occur due to exceptions being thrown during the construction within the allocation via this placement-new function. - dypoperator new
. - Bellocdelete x;
is specified separately (in 5.3.5 [expr.delete]) and only uses the normal deallocation functions. Practical reason: the compiler only knows what arguments to pass to the placement delete while you are in the placement new. - T.C.