Topic: deallocation function lookup


Author: algrant@myrealbox.com (Al Grant)
Date: Thu, 5 Feb 2004 22:06:40 +0000 (UTC)
Raw View
algrant@myrealbox.com (Al Grant) wrote in message news:<5765b025.0402040540.65856bea@posting.google.com>...
> Is the attached program legal?

Apparently not since 12.5#8's rule on static checking of
accessibility would rule out the delete of a1 (base class
deallocation function is private).

But accessibility is not an issue in the delete of a2.
VC++ 7.1 accepts this.  Others don't.  Is it legal C++?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: algrant@myrealbox.com (Al Grant)
Date: Wed, 4 Feb 2004 15:29:58 +0000 (UTC)
Raw View
Is the attached program legal?  The issue is 12.5#4:

  if a delete-expression is used to deallocate a class object whose
  static type has a virtual destructor, the deallocation function
  is the one found by the lookup in the definition of the dynamic
  type's virtual destructor.

So you should be ok if the dynamic type defines operator delete with
the 'usual' signature, irrespective of base-class definitions.
Most compilers fault both 'deletes' though VC++ 6.0 and 7.1 fault
(a different) one each.

12.5#4 goes on to say:

  ... if the result of the lookup is ambiguous or inaccessible,
  or if the lookup selects a placement deallocation function, the
  program is ill-formed.

Is ill-formed the right word here?  Isn't this more a case of runtime
undefined behaviour?


#include <stdio.h>

struct A1 {
  virtual ~A1() { printf("A1 destructor\n"); }
private:
  void operator delete(void *) {
    printf("A1 deallocation function (private)\n"); }
};

struct A2 {
  virtual ~A2() { printf("A2 destructor\n"); }
  void operator delete(void *, float) {
    printf("A2 deallocation function (non-usual)\n"); }
};

struct B: A1, A2 {
  virtual ~B() { printf("B destructor\n"); }
  void operator delete(void *) { printf("B deallocation function\n"); }
};

int main()
{
  A1 *a1 = new B;
  delete a1;
  A2 *a2 = new B;
  delete a2;
  return 0;
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]