虚拟继承的价格是多少?

What is the price of virtual inheritance?

这好像是个基础问题,不过没看到问过:

假设以下简单情况:

就访问最派生的成员所需的时间而言,虚拟继承的代价是多少 class?特别是,如果价格不为零,它是仅适用于通过多条路径继承的成员还是也适用于其他成员?

访问虚拟基class的数据成员是通过一个额外的间接寻址。

What is the price of virtual inheritance in terms of the time needed to access the members of the most derived class?

一次偏移查找和一次加法(2 条指令和一次内存获取)

In particular, if there is a non-zero price, does it pertain only to the members that are inherited through more than one path or to other members as well?

是的,但并非总是如此。如果编译器有足够的信息证明不需要通过间接访问,则可以在编译时将查找短路。

It'd probably be good to clarify exact when this would be the case. – Nicol Bolas

先生说得好

这里有一个例子来证明这一点。使用 -O2 和 -S 选项编译以查看优化效果。

#include <memory>
#include <string>

enum class proof {
    base,
    derived
};

// volatile forces the compiler to actually perform reads and writes to _proof
// Without this, if the compiler can prove that there is no side-effect  of not performing the write,
// it can eliminate whole chunks of our test program!

volatile proof _proof;

struct base
{
    virtual void foo() const {
        _proof = proof::base;
    }

    virtual ~base() = default;
};

struct derived : base
{
    void foo() const override {
        _proof = proof::derived;
    }
};

// factory function
std::unique_ptr<base> make_base(const std::string&name)
{
    static const std::string _derived = "derived";

    // only create a derived if the specified string contains
    // "derived" - on my compiler this is enough to defeat the
    // optimiser

    if (name == _derived) {
        return std::make_unique<derived>();
    }
    else {
        return {};
    }
}

auto main() -> int
{
    // here the compiler is fully aware that p is pointing at a derived
    auto p = std::make_unique<derived>();

    // therefore the call to foo() is made directly (in fact, clang even inlines it)
    p->foo();

    // even here, the compiler 'knows' that b is pointing at a 'derived'
    // so the call to foo is made directly (and indeed on my compiler, completely
    // inlined)
    auto b = std::unique_ptr<base>(new derived);
    b->foo();

    // here we assign a derived to b via indirect construction through a string.
    // Unless the compiler is going to track this string and follow the logic in make_base
    // (and on my compiler it does not) this will prevent the virtual call to foo() from
    // being turned into a direct call.
    // Therefore, this call will be made via the virtual function table of *b
    b = make_base("derived");
    if (b) {
        b->foo();
    }

    return 0;
}