user5545734
user5545734

Reputation:

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": http://goo.gl/Ax6oth

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

Upvotes: 7

Views: 3300

Answers (2)

ZMJ
ZMJ

Reputation: 437

I encountered same problem. @decltype_auto said it's a C++11 feature. So I use option "-std=c++98" to bypass the problem.

Upvotes: -2

decltype_auto
decltype_auto

Reputation: 1736

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.

Upvotes: 10

Related Questions