When would these give a different answer, and when would this difference be useful, if at all?
1 Answers
The former tests for the trivially copyable property, which in few words means that the type is memcpy
-safe.
A trivially copyable class is a class that:
— has no non-trivial copy constructors (12.8),
— has no non-trivial move constructors (12.8),
— has no non-trivial copy assignment operators (13.5.3, 12.8),
— has no non-trivial move assignment operators (13.5.3, 12.8), and
— has a trivial destructor (12.4).
A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable.
[ Note: In particular, a trivially copyable or trivial class does not have virtual functions or virtual base classes.—end note ]
The latter tests for the presence of a trivial copy constructor, which incidentally is a requirement for the trivially copyable property. It basically implies that the copy constructor for the type performs a bitwise copy.
A copy/move constructor for class X is trivial if it is not user-provided and if
— class X has no virtual functions (10.3) and no virtual base classes (10.1), and
— the constructor selected to copy/move each direct base class subobject is trivial, and
— for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial;
otherwise the copy/move constructor is non-trivial.
It is easy to fabricate a type that provides different results for these traits:
struct foo {
foo(foo const&) = default; // this is a trivial copy constructor
~foo(); // this is a non-trivial destructor
};

- 228,013
- 71
- 433
- 510
-
@Dave No. And SO wants me to say that with 15 characters. – R. Martinho Fernandes May 14 '13 at 11:50
-
2You can find more information about these properties on my comprehensive answer about [PODs in C++11](http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special/7189821#7189821). – R. Martinho Fernandes May 14 '13 at 11:52
-
I think it would be interesting to see examples of when the two traits give different results–like the question calls for–to illustrate the difference more directly. Can you think of any examples? – Magnus Hoff May 14 '13 at 11:57
-
@Dave Look at the listed requirements, as can be seen it's only the first requirement in the list and therefore one of many. – Christian Rau May 14 '13 at 12:50
-
@ChristianRau I commented before he edited to add those lists. – David May 14 '13 at 12:52
-
wouldn't `is_trivially_copy_constructible` by itself be sufficient to be `memcpy`/`memmove`/`realloc` safe?? – Martin Ba Mar 03 '16 at 14:44
-
Actually, `foo` **won't provide different results** in many implementations. See, [LWG2827](https://cplusplus.github.io/LWG/lwg-active.html#2827). – Daniel Langr Sep 16 '20 at 15:25