[英]Error with copy constructor/assignment operator for a class which has std::atomic member variable
I have a class like below. 我有一个类似下面的课程。
#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;
}
I am getting compilation error like this, 我收到这样的编译错误,
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>&)’
Is that the default copy constructor and assignment opertaor do not work in such case? 这是默认的复制构造函数,赋值opertaor在这种情况下不起作用吗?
PS: I am using gcc version 4.6.3 PS:我使用的是gcc 4.6.3版
You can't copy atomics with a standard copy constructor, since all loads and stores must happen explicitly. 您不能使用标准复制构造函数复制原子,因为所有加载和存储必须显式发生。 You'll have to write your own copy constructor for
Sequence
which does some initialization of the form value_(rhs.value_.load())
(possibly with more relaxed memory ordering). 你必须为
Sequence
编写自己的拷贝构造函数,它对表单value_(rhs.value_.load())
一些初始化(可能有更宽松的内存排序)。
Atomic has deleted copy-ctor. Atomic删除了copy-ctor。 So copy/move-ctors in your class are deleted.
因此,您班级中的复制/移动控制器将被删除。
n3337 12.8/11 n3337 12.8 / 11
An implicitly-declared copy/move constructor is an inline public member of its class.
隐式声明的复制/移动构造函数是其类的内联公共成员。 A defaulted copy/ move constructor for a class X is defined as deleted (8.4.3) if X has:
如果X具有以下内容,则将类X的默认复制/移动构造函数定义为已删除(8.4.3):
— a non-static data member of class type M (or array thereof) that cannot be copied/moved because overload resolution (13.3), as applied to M's corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor,
- 类型M(或其数组)的非静态数据成员,由于应用于M的相应构造函数的重载解析(13.3),无法复制/移动,导致模糊或被删除或无法访问的函数默认构造函数,
Because there is no std::atomic<long int>::atomic(const std::atomic<long int>&)
function, there is no way for the compiler to create a default copy constructor for the Sequence
class. 因为没有
std::atomic<long int>::atomic(const std::atomic<long int>&)
函数,所以编译器无法为Sequence
类创建默认的复制构造函数。 If you need a copy constructor for that class (and you do if you want Sequence secondSequence = firstSequence;
to work) then you need to write one. 如果您需要该类的复制构造函数(如果您需要
Sequence secondSequence = firstSequence;
可以工作),那么您需要编写一个。
This behavior is required by the standard: 标准需要此行为:
The atomic integral and address types are listed below.
原子积分和地址类型如下所示。 These types shall have standard layout.
这些类型应具有标准布局。 They shall have a trivial default constructor, A constexpr explicit value constructor, a deleted copy constructor , a deleted copy assignment operator, and a trivial destructor.
它们应该有一个简单的默认构造函数,一个constexpr显式值构造函数, 一个删除的复制构造函数 ,一个删除的复制赋值运算符和一个普通的析构函数。 These types shall support aggregate initialization syntax.
这些类型应支持聚合初始化语法。
I'd guess that choice to delete the copy constructor in the standard was for two reasons: 我想在标准中删除复制构造函数的选择有两个原因:
a load/store pair is required in general. 通常需要加载/存储对。 Is there any way to enforce that this would be done when you don't control the callers of std::atomic?
当你不控制std :: atomic的调用者时,有没有办法强制执行这个操作?
What do you do if std::atomic<> type that you were using was one for which is_lock_free() is false (ie. a mutex is required in the implementation for that size integer type)? 如果你使用的std :: atomic <>类型是is_lock_free()为false的那个(即在该大小整数类型的实现中需要互斥锁),你会怎么做? What copy semantics do you use for the mutex initialization?
您使用什么复制语义进行互斥初始化? A mutex that ends up implicitly copied needs to be re-initialized since it could be unluckily copied in a locked state.
最终隐式复制的互斥锁需要重新初始化,因为它可能在锁定状态下被不幸地复制。 I'd guess that std::mutex also has a deleted copy constructor because of this, and that pushes the requirement into std::atomic too.
我猜测std :: mutex也有一个已删除的拷贝构造函数,因为这样,它也将需求推送到std :: atomic。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.