4

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;
}

demo

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?

  • Exactly which version of which compiler are you using; exactly how did you invoke the compiler; and which version of the C++ standard are you quoting? – zwol Jul 24 '14 at 17:40
  • delete[] expressions that use global array deallocation functions, always call single-argument signatures – Igor Pejic Jul 24 '14 at 17:40
  • Also, which version of the standard are you looking at? I don't see that anywhere. – Joseph Mansfield Jul 24 '14 at 17:41
  • @JosephMansfield I work with N3797. I've updated my Q. –  Jul 24 '14 at 17:43
  • @ShafikYaghmour By the way, has N3936 already been published? –  Jul 24 '14 at 17:45
  • Okay, this isn't in the C++11 standard. It looks like it might be a bug with the C++11 standard, because I can't see any mention of it. The compilers are just making a guess at what to do. The text you quoted is in both N3797 and N3936. Might be worth looking through the issues lists over the last few years. – Joseph Mansfield Jul 24 '14 at 17:46
  • 1
    @DmitryFucintv it is available via github see [Where do I find the current C or C++ standard documents?](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents#comment36876555_4653479) – Shafik Yaghmour Jul 24 '14 at 18:03

3 Answers3

5

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

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.

Khouri Giordano
  • 1,426
  • 15
  • 17