gpt4 book ai didi

c++ - 虚拟继承的价格是多少?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:13 24 4
gpt4 key购买 nike

这似乎是一个基本问题,但我没有看到它被问到:

假设以下简单情况:

  • 没有虚拟成员。

  • 虚拟继承用于允许多个路径指向同一基。

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

最佳答案

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;
}

关于c++ - 虚拟继承的价格是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34256074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com