Skip to content

final does not cause devirtualization of nested calls #42985

Open
@apolukhin

Description

@apolukhin
Bugzilla Link 43640
Version trunk
OS Linux
CC @apolukhin,@dwblaikie,@DougGregor,@efriedma-quic,@zygoloid

Extended Description

Consider the example:

struct A {
    virtual int f() { return 0; }
    virtual int g() { return f() + 40; } 
};

struct B2 final : A {
    int g() override;
};
int B2::g() { return A::g(); }

B2 is final, so any call to the virtual functions of A end up with a call to the same function in B2. So B2::g() should inline the A::g() and get optimized to:

int B2::g() { return B2::f() + 40; }

Which is just 40, because B2::f() always returns 0.

Godbolt playground: https://godbolt.org/z/skqgH8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions