gpt4 book ai didi

c++ - 用用户定义的UDT类型实例化std::atomic <>。如果UDT具有虚拟功能,则墨水会失败。为什么?

转载 作者:行者123 更新时间:2023-12-02 09:56:35 27 4
gpt4 key购买 nike

这是一个例子:

#include <atomic>
using namespace std;

struct udt
{
int a;
virtual int get()
{
return a;
}
};

main()
{
udt y;
atomic<udt> x;
x=y;
}

在链接阶段使用gcc编译程序失败,错误消息是 未定义对`__atomic_store_16'的引用。删除udt::get()的虚拟键盘可以解决此问题。我不明白为什么udt用于实例化std::atomic <>时不具有虚函数

最佳答案

[atomics.types.generic]/1 The template argument for T shall be trivially copyable...

[class]/6 A trivially copyable class is a class:
(6.1) — where each copy constructor, move constructor, copy assignment operator, and move assignment operator (15.8, 16.5.3) is either deleted or trivial...

[class.copy.ctor]/11 A copy/move constructor for class X is trivial if it is not user-provided and if:
(11.1) — class X has no virtual functions (13.3) and no virtual base classes (13.1)...


udt不可复制。通过将模板参数传递给标准库中的模板,您的程序表现出未定义的行为,这违反了该模板的要求。

关于c++ - 用用户定义的UDT类型实例化std::atomic <>。如果UDT具有虚拟功能,则墨水会失败。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59591158/

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