Deleting virtual functions in C++0x
Asked Answered
S

2

21

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?

Saxtuba answered 11/10, 2010 at 21:25 Comment(3)
@GMan I agree that there's probably no sane reason but the proposal seems to explicitely allow it.Laritalariviere
@flownt: Oops, sorry. :) Deleted on you.Sidky
Would it force the creation of RTTI. The equivalent in current C++ is to have an empty virtual destructor?Kyser
L
17

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.

Laritalariviere answered 11/10, 2010 at 22:33 Comment(6)
does it force RTTI info to be stored?Rellia
So it's like my deleted answer?Lucianaluciano
@Klaim: how would i vieuw your deleted answer?Laritalariviere
Copy/paste : My understanding (that might really be wrong, but please tell us if it is) is that: if it's a base class, it's exactly the same as declaring a virtual pure function ( using =0); if it's a child class and deriv_func() already have been declared has virtual, it's like making the child implementation null, making the child class an abstract class that force the child of this child class to implement the function....Lucianaluciano
perhaps i misread it but as it stands in the answer it seems to imply that it's not exactly the same as =0 and g++ seems to confirm it though i can't get it to compile the example because it thinks i'm using the function(seems like a bug).Laritalariviere
This sometimes becomes useful if you want to kill some overloads across the class hierarchy. E.g. class A { virtual void foo(const int& x) = 0; virtual void foo(int&&) = delete; }Sandysandye
A
5

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.

Anglo answered 24/8, 2012 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.