gpt4 book ai didi

c++ - 为什么没有用户提供 cp/mv ctor 并具有虚函数但没有虚拟基的类没有 "trival cp/mv ctor"?

转载 作者:搜寻专家 更新时间:2023-10-31 00:53:12 29 4
gpt4 key购买 nike

根据标准:

A copy/move constructor for class X is trivial if it is not user-provided and if

— class X has no virtual functions (10.3) and no virtual base classes (10.1), and

— the constructor selected to copy/move each direct base class subobject is trivial, and

— for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial;

otherwise the copy/move constructor is non-trivial.

我认为该标准引入了“trival cp/mv ctor”的概念来推断您可以只使用 std::memcpy 复制类而不是调用构造函数,并且不会有未定义的行为。

但是,标准不允许存在虚函数,我认为虚函数有违“trival cp/mv ctor”的精神。具有指向虚函数的 vtable 的类仍然可以使用 std::memcpy 进行复制并且具有正确的行为。毕竟,您不能在运行时更改类的 vtable——这会破坏此类的其他实例。

那么,为什么没有用户提供的 cp/mv ctor 和具有虚函数但没有虚拟基的类不能有“trival cp/mv ctor”?

最佳答案

这不是一个简单的类型,因为确保“vptr”指向正确的“vtable”并不像复制指针的值那么简单。我们可以复制出只是基础子对象。我们不需要总是处理最派生的对象类型。

void bar(base const& b) { 
b.overriden_function();
}

void foo(base const& b) {
auto other_b = b;
bar(other_b);
}

int main() {
derived d;
foo(d);
}

让我们假设 base 是微不足道的。这样copy就照你说的做了。 b 的 vptr 指向 derived 的 vtable。所以现在我们获得了一个对象,它的 vtpr 指向了错误的 vtable。我们调用一个覆盖函数。

轰!

关于c++ - 为什么没有用户提供 cp/mv ctor 并具有虚函数但没有虚拟基的类没有 "trival cp/mv ctor"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473684/

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