packon.h.xml 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <chapter xml:id="packon.h">
  2. <title><tt>__vic/packon.h</tt> &amp; <tt>__vic/packoff.h</tt></title>
  3. <p>Inclusion of the first file turns off struct members alignment. In other
  4. words, turns on "structures packing" - size of the struct is strictly a sum of
  5. its members sizes. Inclusion of the second one restores the default alignment.
  6. So that the pair of <tt>#include</tt> directives forms a section in the source
  7. file where structs alignment is disabled.</p>
  8. <note>Each <tt>#include&lt;__vic/packon.h></tt> must have the corresponding
  9. <tt>#include&lt;__vic/packoff.h></tt>.</note>
  10. <section><title>Example</title>
  11. <code-block lang="C++"><![CDATA[
  12. struct unpacked
  13. {
  14. bool f1;
  15. int f2;
  16. };
  17. static_assert(sizeof(unpacked) >= sizeof(bool) + sizeof(int),
  18. "Total struct size can exceed the sum of members sizes");
  19. #include<__vic/packon.h> // alignment disabled starting from here
  20. struct packed
  21. {
  22. bool f1;
  23. int f2;
  24. };
  25. static_assert(sizeof(packed) == sizeof(bool) + sizeof(int),
  26. "Total struct size is exactly the sum of members sizes");
  27. #include<__vic/packoff.h> // alignment enabled again
  28. ]]></code-block>
  29. </section>
  30. </chapter>