1234567891011121314151617181920212223242526272829303132333435363738 |
- <chapter xml:id="packon.h">
- <title><tt>__vic/packon.h</tt> & <tt>__vic/packoff.h</tt></title>
- <p>Inclusion of the first file turns off struct members alignment. In other
- words, turns on "structures packing" - size of the struct is strictly a sum of
- its members sizes. Inclusion of the second one restores the default alignment.
- So that the pair of <tt>#include</tt> directives forms a section in the source
- file where structs alignment is disabled.</p>
- <note>Each <tt>#include<__vic/packon.h></tt> must have the corresponding
- <tt>#include<__vic/packoff.h></tt>.</note>
- <section><title>Example</title>
- <code-block lang="C++"><![CDATA[
- struct unpacked
- {
- bool f1;
- int f2;
- };
- static_assert(sizeof(unpacked) >= sizeof(bool) + sizeof(int),
- "Total struct size can exceed the sum of members sizes");
- #include<__vic/packon.h> // alignment disabled starting from here
- struct packed
- {
- bool f1;
- int f2;
- };
- static_assert(sizeof(packed) == sizeof(bool) + sizeof(int),
- "Total struct size is exactly the sum of members sizes");
- #include<__vic/packoff.h> // alignment enabled again
- ]]></code-block>
- </section>
- </chapter>
|