10

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.

10
  • Does this more up-to-date definition help at all? Commented Aug 1 at 2:53
  • @user4581301, so the part I mainly concern is the "trivially copyable class types" category in the link. TWIT, the "cv-qualified versions of these types" and "arrays of such types" of "scalar types" certainly also is identical/applies to C, I suppose. Commented Aug 1 at 2:56
  • 6
    No. struct A { int a; static int b; }; is trivially copyable, but not a valid C struct. Commented Aug 1 at 2:58
  • @3CEZVQ, oh... indeed, C does not allow static storage for struct member IIRC. Many thanks for this straightforward example. Commented Aug 1 at 3:03
  • 1
    Another counter example template<std::integral T> struct my_struct { T data; };, trivially copyable but yet... not a "C" struct Commented Aug 1 at 3:23

1 Answer 1

21

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In terms of actual data layout for an instance of such an object, a C struct with the members in some order should match the layout of a C++ class or struct. But the C struct order that matches might vary by C++ ABI in terms of whether private members are grouped together, etc. Base-class members of course go first. The language-level differences are important (especially base classes), so good answer, but I just wanted to add an assembly-language perspective. (C's common-initial-subsequence rule for compatibility of structs in unions might be relevant if actually writing hacky code...)
I've thought that a struct with an owning pointer is not trivially-copyable, but in C++ the ownership is established via a destructor and C struct would not have any.
You kinda said it yourself. As far as the language is concerned, there is no such thing as "a struct with an owning pointer". The language only cares about "a struct with a non-trivial destructor". It doesn't care why the destructor is non-trivial; it doesn't look for "owning pointers" or somesuch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.