Questions tagged [trivially-copyable]
15 questions
14
votes
3 answers
Why is a class trivially copyable with all private special member functions?
Code is like:
#include
#include
class A
{
A() = default;
A(const A&) = default;
A(A&&) = default;
A& operator=(const A&) = default;
A& operator=(A&&) = default;
~A() = default;
int a;
};
int…

o_oTurtle
- 1,091
- 3
- 12
8
votes
1 answer
Why would trivial copy/move contructibility depend on trivial destructibility?
A class with all special functions defaulted except a non-trivial destructor is not trivially move or copy constructible. See https://godbolt.org/z/o83rPz for an example:
#include
class Sample
{
public:
Sample(Sample const&) =…

Michael Steffens
- 179
- 7
6
votes
2 answers
mutable data member, template constructor and trivially copy constructible
Example code could be found below or on godbolt. Say we have 4 classes:
S: holding a data member.
SCtor: holding a data member and has a template constructor.
SCtorMutable: holding a mutable data member and has a template…

wanghan02
- 1,227
- 7
- 14
5
votes
3 answers
Trivially copyable class - what has changed in C++20?
The standard says
A trivially copyable class is a class:
(1.1) that has at least one eligible copy constructor, move
constructor, copy assignment operator, or move assignment operator
([special], [class.copy.ctor], [class.copy.assign]),
(1.2) where…

Myrddin Krustowski
- 701
- 3
- 13
4
votes
0 answers
Object pointer (this) saved in the constructor for later use seems to have incorrect value
I came across a strange bug today, which basically boils down to the following minimal example:
#include
struct S* ps;
struct S{
S(){
ps = this;
}
//~S(){} //(*)
};
S makeS(){
return S{};
}
int main(){
S…

Weijun Zhou
- 746
- 1
- 7
- 25
3
votes
2 answers
Why (if that is the case) does the standard say that copying uninitialized memory with memcpy is UB?
When a class member cannot have a sensible meaning at the moment of construction,
I don't initialize it. Obviously that only applies to POD types, you cannot NOT
initialize an object with constructors.
The advantage of that, apart from saving CPU…

Carlo Wood
- 5,648
- 2
- 35
- 47
2
votes
1 answer
std::is_trivially_copyable is too strong, what shall I use instead?
I want to accept classes which are "trivially copyable", in the sense that if I mem-copy the byte representation of one variable of the type into another, it will be usable, and nothing will have been broken in any way.
Looking at…

einpoklum
- 118,144
- 57
- 340
- 684
1
vote
1 answer
Class isn't trivially_copyable if a constraint on its assignment operator is not satisfied with clang 16
Example code as below or on godbolt. clang 16/trunk believes S is not a trivially_copyable class. clang 15, gcc trunk and MSVC believe otherwise.
#include
template
struct S {
T m_t;
S(S const&) = default;
…

wanghan02
- 1,227
- 7
- 14
1
vote
1 answer
How to define a type so it can be static initialized?
I have been learning about trivial and standard layout types. I think I understand the basics behind it, but there is still something I am missing.
Please have a look at the two following examples:
Example 1:
int main()
{
struct SomeType
{
…

Nik Polin
- 11
- 3
1
vote
0 answers
copy assignment operator with volatile qualifier
Below, I used template learnt from @Daniel McLaury in here.
#include
template
struct A_base {
int val;
// template // needs it to make MSVC work
volatile T& operator=(const volatile T& a)…

Home of the Brave
- 133
- 8
1
vote
1 answer
Is std::istream_iterator trivially copy constuctible?
Why does this program compile on MSVC but not on GCC and Clang? godbolt
#include
#include
static_assert(std::is_trivially_copy_constructible_v>);
According to [istream.iterator.cons]/6, the…

cppbest
- 59
- 8
1
vote
0 answers
Trivial class that is not standard layout and vice versa
What's an example of a C++ class that is a Trivial class but is not a Standard Layout class?
And vice versa? (A Standard Layout class that is not a Trivial class).

Michele Piccolini
- 2,634
- 16
- 29
1
vote
3 answers
Is this undefined because I memcpy'ed a non_trivially_copyable type?
I'm having a lot of trouble understanding why memcpy'ing non_copyable types is not allowed, or even if my code in the following is not allowed:
struct trivially_copyable_type
{
int member;
};
struct non_trivially_copyable_type
{
int…

Zebrafish
- 11,682
- 3
- 43
- 119
1
vote
1 answer
How to atomically copy types that are not-trivially copyable
I'm writing an Atom class, for types T are not trivially-copyable. I was wondering if my below implementation of load() and store() could cause a race condition.
class Atom {
// Assumptions:
// 1. atomic is not natively supported
//…

Samuel Okechukwu
- 205
- 1
- 6
0
votes
2 answers
Does the same trivially-copyable limitation that applies to `memcpy()` also apply to `std::copy()`?
Cppreference states, regarding std::memcpy() (emphasis added):
If the objects are potentially-overlapping or not TriviallyCopyable, the behavior of memcpy is not specified and may be undefined.
So, I always check to ensure an object IS…

Gabriel Staples
- 36,492
- 15
- 194
- 265