Clang complains “cannot override a deleted functio

2019-06-20 07:15发布

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?

2条回答
放我归山
2楼-- · 2019-06-20 07:39
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.

查看更多
放荡不羁爱自由
3楼-- · 2019-06-20 07:44

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

查看更多
登录 后发表回答