为什么商店原子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)
这是怎么回事?
你不能用std::unique_ptr
实例化std::atomic
。 cppreference
的std ::原子可以与任何类型TriviallyCopyable T.性病被实例化::原子既不是可复制也不移动。
而一个std::unique_ptr
不TriviallyCopyable
类满足MoveConstructible和MoveAssignable要求,但也不CopyConstructible或CopyAssignable的要求。
你可以使用一个std::shared_ptr
,它具有free functions defined to allow you to have atomic stores and loads
感谢您的回答。那么我是否正确地认为这里的问题是VC++ 2013没有针对'std :: atomic
我不认为有任何要求它检查'T'是否TriviallyCopyable – NathanOliver
@ user2296177我不认为这是一个错误,因为标准调用'T'必须* TriviallyCopyable *所以你的负担是你给它一个正确的类型。 – NathanOliver
什么编译器和库版本?什么CPU架构? –
@BenVoigt VC++ 2013(v120)CPU:i7-4790处理器(x86-64) – user2296177