c++cstructatomictrivially-copyable

Is CPP TrivialCopyable class effectively a C struct?


During coding of std::atomic, CAS, etc, I always struggle to memorize the definition of CPP class being "TriviallyCopyable".

Now I am gradually switching to C world, I accidentally found that most, if not all, scenarios where I deem one class to be TrivialCopyable is effectively a C struct. And the requirements described in the link above indeed looks like a C struct by me.

Is it? what did I miss?

N.B. of course C struct is public by default, etc, but let's just ignore those relatively irrelevant features, instead, my guess is whatever deemed to be TriviallyCopyable in CPP can be made in C by struct, with no hard hacking involved.


Solution

  • While all C structs are trivially copyable in C++, the reverse is not true. But this is mostly a matter of C++ having numerous constructs that C doesn't support which won't change the class's trivial copyability status. This can include non-virtual base classes (trivial copyability doesn't care how many base classes it has), any members that aren't non-static data members (member functions, static data members, constexpr members, constructors that don't interfere with trivial copyability etc), friend declarations, access controls like private and protected, etc.