为什么商店原子unique_ptr导致崩溃?

问题描述:

代码在i7-4790处理器(x86-64)上的VC++ 2013(v120)下编译时没有问题。为什么商店原子unique_ptr导致崩溃?

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

一旦main()回报,我得到一个崩溃:

表达:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)

这是怎么回事?

+0

什么编译器和库版本?什么CPU架构? –

+0

@BenVoigt VC++ 2013(v120)CPU:i7-4790处理器(x86-64) – user2296177

你不能用std::unique_ptr实例化std::atomiccppreference

的std ::原子可以与任何类型TriviallyCopyable T.性病被实例化::原子既不是可复制也不移动。

而一个std::unique_ptr不TriviallyCopyable

类满足MoveConstructibleMoveAssignable要求,但也不CopyConstructibleCopyAssignable的要求。

你可以使用一个std::shared_ptr,它具有free functions defined to allow you to have atomic stores and loads

+0

感谢您的回答。那么我是否正确地认为这里的问题是VC++ 2013没有针对'std :: atomic '对静态类型'T'进行静态断言? – user2296177

+2

我不认为有任何要求它检查'T'是否TriviallyCopyable – NathanOliver

+0

@ user2296177我不认为这是一个错误,因为标准调用'T'必须* TriviallyCopyable *所以你的负担是你给它一个正确的类型。 – NathanOliver