Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 535
PkDrew Avatar asked Apr 17 '26 15:04

PkDrew


1 Answers

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.

like image 175
Nicol Bolas Avatar answered Apr 20 '26 05:04

Nicol Bolas