I have a question that I cannot find a satisfying answer to: Why are references not considered trivially copyable?
std::is_trivially_copyable<int>::value == true
std::is_trivially_copyable<int&>::value == false
Example 1:
int a = 5;
int& b = a;
int c = -1;
std::memcpy(&c, &b, sizeof(b));
assert(c == a);
assert(std::is_trivially_copyable<int&>::value == false);
This works exactly as expected.
Example 2:
struct A
{
int foo;
};
void test(const A& a)
{
A a2{};
std::memcpy(&a2, &a, sizeof(a));
assert(std::is_trivially_copyable<const A&>::value == false);
assert(std::memcmp(&a, &a2, sizeof(A)) == 0);
}
int main()
{
assert(std::is_trivially_copyable<A>::value);
A a{1};
test(a);
return 0;
}
Why are references not considered trivially copyable? I must be missing something here.