Clang complains “cannot override a deleted function” while no function is deleted

…衆ロ難τιáo~ 提交于 2019-12-08 19:39:34

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/33626088/clang-complains-cannot-override-a-deleted-function-while-no-function-is-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!