admin管理员组文章数量:1353311
This is more of a theoretical question about the interpretation of the standard in one particular case, given below:
struct B;
struct A {
virtual bool operator ==(const B&) const = delete;
};
struct B : A {
bool operator ==(const B&) const = default;
};
This program is rejected by GCC, EDG, MSVC with the
error: nondeleted function overrides deleted function "A::operator=="
which looks reasonable according to :
A deleted function can only be overridden by deleted functions
But Clang actually admits the program with the
note: defaulted 'operator==' is implicitly deleted because there is no viable 'operator==' for base class 'A'
Online demo:
Which implementation is correct here?
This is more of a theoretical question about the interpretation of the standard in one particular case, given below:
struct B;
struct A {
virtual bool operator ==(const B&) const = delete;
};
struct B : A {
bool operator ==(const B&) const = default;
};
This program is rejected by GCC, EDG, MSVC with the
error: nondeleted function overrides deleted function "A::operator=="
which looks reasonable according to https://en.cppreference/w/cpp/language/function :
A deleted function can only be overridden by deleted functions
But Clang actually admits the program with the
note: defaulted 'operator==' is implicitly deleted because there is no viable 'operator==' for base class 'A'
Online demo: https://gcc.godbolt./z/n83nGE7dW
Which implementation is correct here?
Share Improve this question asked Mar 31 at 18:40 FedorFedor 21.6k37 gold badges55 silver badges168 bronze badges 5 |1 Answer
Reset to default 3The standard is just vauge enough on this to make me accept both interpretations (of Clang vs the others) as valid. Because as written we have
[class.virtual]
17 A deleted function ([dcl.fct.def]) shall not override a function that is not deleted. Likewise, a function that is not deleted shall not override a deleted function.
The standard only says that we can't have a mismatch of non-deleted to deleted (implicitly or explicitly - [dcl.fct.def.delete]/1) between base definition and overrider (or vice-versa). What's absent is that there's no explicit requirement for the two functions to be deleted in the same way. I.e. nothing says both have to be explicitly deleted, or implicitly deleted; nothing says we can't have them deleted in different ways, like the OP does.
Now, is it reasonable to allow it as Clang does, with a warning? Yes, it is. We don't violate anything in the standard to warrant a diagnostic (to my reading), and there's an "obvious" meaning to the code since the function is deleted either-way. Furthermore, the warning tells us we might have done somthing we didn't mean to. Clang encourages us to delete explicitly if we wish to keep the overrider and silence the warning.
Is it reasonable to reject the code until we explicitly delete? I'd say "yes" to that as well, it's a small change at the end of the day, that Clang is suggesting as well. Not unreasonable to require the programmer to be explicit.
On the whole, this might warrant a small core issue to get all compilers to the same baseline. But there isn't really a whole lot broken here.
本文标签: cImplicitly deleted function overrides deleted virtual functionStack Overflow
版权声明:本文标题:c++ - Implicitly deleted function overrides deleted virtual function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743927027a2563149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
operator==
can't be implicitly deleted, since it simply can't be overriden to be defaulted. – cigien Commented Mar 31 at 19:06B::operator==
implicitly deleted sounds like a reasonable extension. – Pete Becker Commented Mar 31 at 19:24default
ing it simply make itdelete
d? It's not like it's actually making it callable. If one tries that, clang refuses too. – Ted Lyngmo Commented Mar 31 at 20:45