PROBLEM
I have std :: vector gazillion Foo's
struct Foo { int m_i; char m_c; char m_padding[3];
I can write this piece of contiguous Foo quickly in binary form in just one shot.
My problem is that if I do not explicitly specify this m_padding, compute it and clear it myself, valgrind will complain about uninitialized entries.
Question
Is it possible to write a template class in C ++ 11 that will compute this add-on for me at compile time?
If so, I can simply add it at the end of my whole Foo and automatically initialize / clear them without complaining from valgrind.
I can do this manually by calculating sizeof (padding) = sizeof (Foo) - sum (sizeof (parts)), but it would be nice to create some class for this calculation, since all the information is available at compile time.
For simplicity, suppose Foo has a trivial layout (type_traits is an important but tangent problem). Also, ignore ordering issues / cross-platform issues.
Possible approach
This does not answer my initial question directly, but the hvd suggestion implies a simpler approach that seems to work for some simple test cases I tried:
template<typename T> struct BZero { BZero() { std::memset( this, 0, sizeof( T )); } }; struct Foo : public BZero<Foo> { int m_i; char m_c; };
c ++ sizeof c ++ 11
kfmfe04
source share