I don't understand why the padding after m_data
, when the copy constructor constructs the object, is not initialized to 0 in an optimized build like -O2
. The default constructor always seems to initialize it. I haven't tried yet other special member functions.
Is this behaviour implementation-defined? Not defined?
Is there a way to initialize it?
Thanks
#include <climits>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>
#include <span>
void print(std::span<const std::byte> const span)
{
for (auto const i : span) {
std::cout << std::to_integer<int>(i) << ' ';
}
std::cout << '\n';
}
struct Foo {
Foo()
{}
Foo(const Foo&)
{}
/*alignas(1)*/ std::byte m_data[1] = {};
uint32_t val = INT_MAX;
};
inline auto objAsBytes(const auto& obj)
{
return std::as_bytes(std::span { std::addressof(obj), 1 });
}
int main()
{
auto foo = Foo {};
std::span fooAsBytes = objAsBytes(foo);
std::cout << "fooAsBytes\n";
print(fooAsBytes);
auto fooCopy = foo;
std::span fooCopyBytes = objAsBytes(fooCopy);
std::cout << "fooCopyBytes\n";
print(fooCopyBytes);
Foo fooAssign;
fooAssign = foo;
std::span fooAssignBytes = objAsBytes(fooAssign);
std::cout << "fooAssignBytes\n";
print(fooAssignBytes);
}