c++11language-lawyerclang++overridingvirtual-destructor

Clang complains "cannot override a deleted function" while no function is deleted


In the following simple code fragment:

#include <cstddef>
 
struct B
{
  virtual ~B() = default;
  static void operator delete(void *, int);
  static void * operator new(size_t, int);
};

struct C : B
{
  virtual ~C() = default;
};

clang 3.7 complains that "non-deleted function '~C' cannot override a deleted function"

Neither Visual Studio nor GCC report an error in this code. Is it a clang defect or what?


Solution

  • static void operator delete(void *, int);
    

    No, it's

     static void operator delete(void *, std::size_t);
    

    and that type difference causes an ambiguity that gets relevant:

    cppreference.com has

    The implicitly-declared or defaulted destructor for class T is undefined (until C++11)defined as deleted (since C++11) if any of the following is true:

    [...]

    The implicitly-declared destructor is virtual (because the base class has a virtual destructor) and the lookup for the deallocation function (operator delete() results in a call to ambiguous, deleted, or inaccessible function.

    And in the standard (draft n4140) §12.4 that is

    5 A defaulted destructor for a class X is defined as deleted if:

    [...]

    (5.3) or, for a virtual destructor, lookup of the non-array deallocation function results in an ambiguity or in a function that is deleted or inaccessible from the defaulted destructor.