Reputation:
I've been trying to understand the following behavior:
#include <iostream>
#include <cstdlib>
using namespace std;
struct A
{
void operator delete[](void *p)
{
cout << "delete\n";
::operator delete[](p);
}
void operator delete[](void *p, size_t t)
{
cout << "delete with two arguments\n";
::operator delete[](p);
}
};
int main()
{
A *a = new A[5];
delete [] a;
}
In the example non-placement deallocation function with one parameter is called. But 5.3.6/10 N3797 C++14 working draft said that:
If the type is complete and if deallocation function lookup finds both a usual deallocation function with only a pointer parameter and a usual deallocation function with both a pointer parameter and a size parameter, then the selected deallocation function shall be the one with two parameters.
Is it a bug?
Upvotes: 4
Views: 142
Reputation: 1461
In 3.7.4.2/2 of N3797, it says:
if a class T has a member deallocation function named operator delete[] with exactly one parameter, then that function is a usual (non-placement) deallocation function. If class T does not declare such an operator delete[] but does declare a member deallocation function named operator delete[] with exactly two parameters, the second of which has type std::size_t, then this function is a usual deallocation function.
Since you have the single parameter delete[] function, that is the one that gets called.
Upvotes: 0
Reputation: 158559
So this is a new C++1y rule but we can see from defect report 1788 the language may be changed to the following:
the function to be called is selected as follows:
If the type is complete and if, for the second alternative (delete array) only, the operand is a pointer to a class type with a non-trivial destructor or a (possibly multi-dimensional) array thereof, the function with two parameters is selected.
Otherwise, it is unspecified which of the two deallocation functions is selected.
Upvotes: 2
Reputation: 254631
That rule is new in C++14, and you're compiling under the C++11 standard. Specifying -std=c++14
makes no difference, so presumably that compiler hasn't implemented that particular rule yet. Which isn't particularly surprising, since C++14 hasn't been officially published yet.
Upvotes: 5