Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

PS: I am using gcc version 4.6.3

like image 397
polapts Avatar asked Aug 17 '12 09:08

polapts


People also ask

What type of variables we can pass to copy constructor?

To copy the values, copy constructor is used. Hence the object being passed and object being used in function are different. Explanation: While returning an object we can use the copy constructor. When we assign the return value to another object of same class then this copy constructor will be used.

In which conditions your copy constructor is called?

A copy constructor is called when a new object is created from an existing object, as a copy of the existing object. The assignment operator is called when an already initialized object is assigned a new value from another existing object.

What happens when u pass the object of same class in copy constructor?

If an object is passed as value to the Copy Constructor then its copy constructor would call itself, to copy the actual parameter to the formal parameter.

Why can not we pass an object by value to a copy constructor?

Passing by value (rather than by reference) means a copy needs to be made. So passing by value into your copy constructor means you need to make a copy before the copy constructor is invoked, but to make a copy you first need to call the copy constructor.


2 Answers

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).

like image 169
Kerrek SB Avatar answered Sep 23 '22 17:09

Kerrek SB


Atomic has deleted copy-ctor. So copy/move-ctors in your class are deleted.

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:

— 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,

like image 38
ForEveR Avatar answered Sep 22 '22 17:09

ForEveR