加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么商店出现原子unique_ptr导致崩溃?

发布时间:2020-12-16 10:12:02 所属栏目:百科 来源:网络整理
导读:该代码在i7-4790处理器(x86-64)上的VC 2013(v120)下编译没有问题. int main(){ std::atomicstd::unique_ptrint p; p.store(std::make_uniqueint(5));} 一旦main()返回,我就会崩溃: Expression: _BLOCK_TYPE_IS_VALID(pHead-nBlockUse) 这是怎么回事? 解决
该代码在i7-4790处理器(x86-64)上的VC 2013(v120)下编译没有问题.

int main()
{
    std::atomic<std::unique_ptr<int>> p;
    p.store(std::make_unique<int>(5));
}

一旦main()返回,我就会崩溃:

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

这是怎么回事?

解决方法

您无法使用std :: unique_ptr实例化std :: atomic. cppreference

std::atomic may be instantiated with any 07001 type T. std::atomic is neither copyable nor movable.

并且std::unique_ptr不是TriviallyCopyable

The class satisfies the requirements of 07003 and 07004,but not the requirements of either 07005 or 07006.

您可以使用具有free functions defined to allow you to have atomic stores and loads的std::shared_ptr

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读