使用atomic创建复制构造函数时出错

时间:2017-10-10 06:35:57

标签: c++ atomic copy-constructor assignment-operator

我有以下代码段:

class StampedValue {

public:
  long stamp;
  int value;

  // Copy constructor (Creating error)
  StampedValue(const StampedValue & x) {
    this->stamp = x.stamp;
    this->value = x.value;
  }

  // Overload assignment operator (Creating error)
  StampedValue & operator=(const StampedValue &x) {
     this->stamp = x.stamp, this->value = x.value;
     return *this;
  }

  friend bool operator==(const StampedValue &x, const StampedValue &y);

};

// At some point in code, vector of atomic <StampedValue>
vector<atomic<StampledValue> > v;

// At another point, two simple vectors of <StampedValue>
vector<StampedValue> v1, v2;

// perform operations of v1, v2   
v1 = v2;

为了确保最后一行安全工作,我需要重载StampedValue类的赋值运算符,以便向量分配顺利进行。不知何故,由于存在另一个atomic<StampedValue>向量,我无法重载赋值运算符,也无法定义复制构造函数。我收到了这个错误:

error: static assertion failed: std::atomic requires a trivially copyable type
   static_assert(__is_trivially_copyable(_Tp),

为什么我收到此错误?

编辑:正如评论所指出的那样,默认的copy-c&tor斗将完成这项工作。所以,没有必要定义我自己的。

0 个答案:

没有答案