我有一个像下面这样的类。具有std :: atomic成员变量的类的复制构造函数/赋值运算符的错误
#include <atomic>
static const long myValue = 0;
class Sequence
{
public:
Sequence(long initial_value = myValue) : value_(initial_value) {}
private:
std::atomic<long> value_;
};
int main()
{
Sequence firstSequence;
Sequence secondSequence = firstSequence;
return 0;
}
我越来越喜欢这个编译错误,
test.cpp:21:36: error: use of deleted function ‘Sequence::Sequence(const Sequence&)’
test.cpp:5:7: error: ‘Sequence::Sequence(const Sequence&)’ is implicitly deleted because the default definition would be ill-formed:
test.cpp:5:7: error: use of deleted function ‘std::atomic<long int>::atomic(const std::atomic<long int>&)’
是默认的拷贝构造函数和赋值opertaor不要在这种情况下工作吗?
PS:我使用的gcc版本4.6.3
你能编辑和添加一个编译代码的例子吗?我没有解决这个问题,我试过了: '序列&operator =(const序列&其他){value_ = other.value_.load();返回*这个; }' – 2017-09-28 20:50:14
@VictorLamoine:不知道你做了什么,但一般的做法[作品](https://wandbox.org/permlink/ZsHPDqhrIiybq7qf)。 – 2017-09-28 21:35:18
有关如何实际编写复制构造函数(使用默认的'seq_cst'来加载源代码,但是避免将原子库存储到正在构建的对象中的代价)的详细信息,请参阅[使用原子成员的类的复制构造函数](https://stackoverflow.com/questions/19961043/copy-constructor-for-classes-with-atomic-member/46045691#46045691)。确保这真的是你想要实现的;复制原子通常与共享状态的目的相反。 – 2017-10-13 01:34:14