Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are volatile data members trivially copyable?

Whilst writing this answer I realised that I'm not as confident about my conclusions as I usually would ensure before hitting Post Your Answer.

I can find a couple of reasonably convincing citations for the argument that the trivial-copyability of volatile data members is either implementation-defined or flat-out disallowed:

  • https://groups.google.com/forum/?fromgroups=#!topic/comp.std.c++/5cWxmw71ktI
  • http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48118
  • http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#496

But I haven't been able to back this up in the standard1 itself. Particularly "worrying" is that there's no sign of the proposed wording change from that n3159 issues list in the actual standard's final wording.

So, what gives? Are volatile data members trivially copyable, or not?


1   C++11

like image 282
Lightness Races in Orbit Avatar asked Nov 15 '12 22:11

Lightness Races in Orbit


2 Answers

I'm seeing the following definition for "trivially copyable" (C++11 §3.9, paragraph 9):

...Scalar types, trivially copyable class types, arrays of such types, and cv-qualified versions of these types are collectively called trivially copyable types....

cv-qualified by definition includes const and/or volatile (§3.9.3). It would therefore appear that volatile values are explicitly trivially copyable, if the unqualified type would be trivially copyable (a scalar or trivially copyable class type, or array thereof).

like image 151
cHao Avatar answered Oct 12 '22 23:10

cHao


The answer has been changed by defect reports CWG496 and CWG2094. The latter DR reverts the former.

Now, for a volatile non-static data member,

  • if the member is of a scalar type or possibly multidimentional array there of, doesn't make the class non-trivially-copyable, however
  • if the member of is of a class type or possibly multidimentional array there of, the implictly declared copy/move functions of the containing class are all deleted, which makes the class non-trivially copyable (since CWG1734).
like image 22
F.v.S. Avatar answered Oct 13 '22 00:10

F.v.S.