Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is __cxa_deleted_virtual called

Tags:

c++

libstdc++

I try to build a little test case set for avr c++ builds.

There are some "exceptional functions" provided typically from the c++ library. Now I want to write a test program which produces this wrong code which must link to __cxa_deleted_virtual.

Can anyone provide a code snippet which results in linking to that function?

I have actually no idea how to produce this "buggy" code.

like image 539
Klaus Avatar asked Jun 02 '15 12:06

Klaus


1 Answers

It's used to fill the vtable slot of a virtual function that has been defined as deleted:

struct B { virtual void f() = delete; };
struct D : B { virtual void f() = delete; };

(The reason a deleted virtual function is included in the vtable is that this allows it to be later changed to non-deleted without breaking vtable layout.)

I'm not aware of any way it could actually get called in (relatively sane) C++, since the only thing that can override a function with a deleted definition is another function with a deleted definition ([class.virtual]/16), and any attempt to call a function with a deleted definition renders the program ill-formed. I suppose you can invoke the specter of ODR violations:

// TU 1
struct B { virtual void f() = delete; virtual void g(); };
void B::g() { } // causes vtable to be emitted in this TU

// TU 2
struct B { virtual void f(); virtual void g(); };

void h(B* b) { b->f(); }

int main() {
    B b;
    h(&b);
}
like image 108
T.C. Avatar answered Oct 30 '22 21:10

T.C.