删除 C++0x 中的虚拟函数

发布于 2024-09-27 04:36:16 字数 166 浏览 4 评论 0原文

目前尚不清楚如果我删除 C++0x 中的虚拟方法会发生什么:

 virtual int derive_func() = delete;

这是否意味着该类以及从它继承的所有内容都无法定义/实现 derive_func() 方法?或者这是非法/编译错误?

It isn't clear what happens if I delete a virtual method in C++0x:

 virtual int derive_func() = delete;

Does this mean this class and everything that inherits from it can not define/implement the derive_func() method? Or is this illegal/compile error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2024-10-04 04:36:16

http://www.open-std .org/jtc1/sc22/wg21/docs/papers/2007/n2326.html#delete
删除的虚拟函数不能覆盖未删除的虚拟函数,反之亦然。
这意味着它非常无用(至少正如我读到的那样)唯一有效的用途是:

struct A{
     virtual void b() = delete;
};
struct B:A{
     virtual void b() = delete;
};

这是完全无用的,因为该函数永远无法被调用。对于非虚函数,使用更合理

编辑
需要完全明确的是,这是唯一可能的关系,子级可能不会实现,并且您可能无法删除未删除的继承虚拟。

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2326.html#delete
A deleted virtual function may not override a non-deleted virtual function and vice-versa.
meaning its pretty useless (as i read it at least) the only valid use would be:

struct A{
     virtual void b() = delete;
};
struct B:A{
     virtual void b() = delete;
};

which is completely useless, since the function can never be called. for non virtual functions the use is more justified

EDIT
to be completely clear this is the ONLY possible relation, children may not implement and you may not delete a non-deleted inherited virtual.

想你只要分分秒秒 2024-10-04 04:36:16

flownt 说得对,但我想指出的是,在最终的 C++11 草案(N3337)中,相应的语言已移至第 10.3#16 节:

具有已删除定义的函数不得覆盖函数
没有删除的定义。同样,一个函数
没有删除的定义不得覆盖函数
已删除的定义。2


对我来说(第 8.4.3#1 节)似乎相当清楚,删除的定义实际上可以算作定义,实际上是一个内联定义,这意味着删除的定义满足 10.3#11:

应定义或声明在类中声明的虚函数
在该类别中纯正,或两者兼而有之;但不需要诊断。2

然而,当前的实现似乎不一致。这是我的测试用例:

struct Base {
    virtual void bar();
    virtual void foo() = delete;
};

void Base::bar() { }  // a definition of the first non-inline virtual function
int main() { Base b; }
  • Clang 生成一个不可链接的程序:Base::fooBase 的 vtable 中提到。如果你交换 foobar 的顺序,链接器会抱怨整个 vtable 丢失了(因为 Clang 认为 foo 是一个非-没有定义的内联函数)。 我将此作为错误提交;我们将看看开发人员的想法。

  • GCC 抱怨在创建 vtable 时在翻译单元末尾“使用”了 foo;但它确实正确地将 bar 识别为第一个非内联虚拟成员函数,无论 foobar 的顺序如何。

flownt got it right, but I want to point out that in the final C++11 draft (N3337), the corresponding language has moved to section 10.3#16:

A function with a deleted definition shall not override a function
that does not have a deleted definition. Likewise, a function that
does not have a deleted definition shall not override a function with
a deleted definition.2

It seems fairly clear to me (section 8.4.3#1) that a deleted definition does in fact count as a definition, and in fact an inline definition, which means a deleted definition satisfies 10.3#11:

A virtual function declared in a class shall be defined, or declared
pure in that class, or both; but no diagnostic is required.2

However, it seems that current implementations disagree. Here's my test case:

struct Base {
    virtual void bar();
    virtual void foo() = delete;
};

void Base::bar() { }  // a definition of the first non-inline virtual function
int main() { Base b; }
  • Clang produces an unlinkable program: Base::foo is mentioned in the vtable for Base. And if you swap the order of foo and bar, the linker complains that the entire vtable is missing (because Clang thinks foo is a non-inline function with no definition). I filed this as a bug; we'll see what the developers think.

  • GCC complains about a "use" of foo at the end of the translation unit, when it creates the vtable; but it does correctly identify bar as the first non-inline virtual member function, no matter the order of foo and bar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文