最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - copy and assignment operator with constant size arrays - Stack Overflow

programmeradmin8浏览0评论

Suppose this class in C++

class Message {
  char msg[64];
};

Why are operator= and default copy already correct

Message & operator=(const Message &o) {
  if (this != &o)
    this->msg = o.msg;
  }
  return *this;
}

In assignment should be wrong?

Isn't it the default semantics to use assignment operator field per field to implement default assignment and copy construction

Suppose this class in C++

class Message {
  char msg[64];
};

Why are operator= and default copy already correct

Message & operator=(const Message &o) {
  if (this != &o)
    this->msg = o.msg;
  }
  return *this;
}

In assignment should be wrong?

Isn't it the default semantics to use assignment operator field per field to implement default assignment and copy construction

Share Improve this question edited Jan 17 at 17:18 Yann TM asked Jan 17 at 15:04 Yann TMYann TM 2,07515 silver badges23 bronze badges 11
  • 2 Please provide a minimal reproducible example with godbolt.. We don't know what is o.mg – Louis Go Commented Jan 17 at 15:07
  • "For non-union class types, the [Implicitly-defined copy assignment] operator performs member-wise copy assignment of the object's direct bases and non-static data members, in their initialization order, using built-in assignment for the scalars, memberwise copy-assignment for arrays, and copy assignment operator for class types (called non-virtually)." from cppreference, emphasis mine. – paleonix Commented Jan 17 at 15:13
  • 1 @YannTM The standard doesn't say that there's a case that uses memcpy. In any case, the member that's an array of objects that are not trivially copyable works just as well. – Igor Tandetnik Commented Jan 17 at 15:25
  • 1 @YannTM "sizeof is known" is the only possibility in C++, there are no Variable Length Arrays in standard. And tip for future questions: if you want answers based on standard, add language-lawyer tag to your question (added here now). – Yksisarvinen Commented Jan 17 at 15:25
  • 2 This question is similar to: Indirect array assignment works, while direct array assignment does not. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – paleonix Commented Jan 17 at 15:34
 |  Show 6 more comments

1 Answer 1

Reset to default 9

The implicitly-defined copy assignment operator is not as simple as "apply assignment to each member". Arrays are specifically taken into account as a special case.

[class.copy.assign]/12 The implicitly-defined copy/move assignment operator for a non-union class X performs memberwise copy/move assignment of its subobjects... Each subobject is assigned in the manner appropriate to its type:
...
(12.2) if the subobject is an array, each element is assigned, in the manner appropriate to the element type;
...

发布评论

评论列表(0)

  1. 暂无评论