Boost C++ Libraries: Ticket #3731: array without padding https://svn.boost.org/trac10/ticket/3731 <p> Sometimes you have a struct with array in it: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#pragma pack(push, 1)</span> <span class="k">struct</span> <span class="n">X</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">i</span><span class="p">[</span><span class="mi">10</span><span class="p">];</span> <span class="p">};</span> <span class="cp">#pragma pack(pop)</span> </pre></div></div><p> And you want to use boost::array : </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#pragma pack(push, 1)</span> <span class="k">struct</span> <span class="n">X</span> <span class="p">{</span> <span class="n">boost</span><span class="o">::</span><span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="mi">10</span><span class="o">&gt;</span> <span class="n">i</span><span class="p">;</span> <span class="p">};</span> <span class="cp">#pragma pack(pop)</span> </pre></div></div><p> The problem that boost::array is not surrounded with #pragma pack. </p> <p> It would be nice to add a new class boost::array_no_padding with #pragma pack surrounded and with static assert for size checking. </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#if !BOOST_SUPPORTS_PRAGMA_PACK &amp;&amp;</span> <span class="o">!</span><span class="n">BOOST_SUPPORTS_ALIGNMENT_PREFIX</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">BOOST_SUPPORTS_ALIGNMENT_SUFFIX</span> <span class="cp">#error Cannot specify alighnemnt</span> <span class="cp">#endif</span> <span class="k">namespace</span> <span class="n">boost</span> <span class="p">{</span> <span class="cp">#if BOOST_SUPPORTS_PRAGMA_PACK</span> <span class="cp">#define ALIGNMENT_SET</span> <span class="cp">#pramga pack(push, 1)</span> <span class="cp">#endif</span> <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="kt">size_t</span> <span class="n">N</span><span class="o">&gt;</span> <span class="k">class</span> <span class="err">#</span><span class="nc">if</span> <span class="o">!</span><span class="n">defined</span><span class="p">(</span><span class="n">ALIGNMENT</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">BOOST_SUPPORTS_ALIGNMENT_PREFIX</span> <span class="c1">// Could be __declspec(align(1))</span> <span class="n">BOOST_ALIGNMENT_PREFIX</span> <span class="cp">#define ALIGNMENT_SET</span> <span class="cp">#endif</span> <span class="n">array_no_padding</span> <span class="cp">#if !defined(ALIGNMENT) &amp;&amp; BOOST_SUPPORTS_ALIGNMENT_SUFFIX</span> <span class="c1">// Could be __attribute__((aligned(1)))</span> <span class="n">BOOST_ALIGNMENT_SUFFIX</span> <span class="cp">#define ALIGNMENT_SET</span> <span class="cp">#endif</span> <span class="c1">// Can derive from boost::array or copy-paste all the content</span> <span class="o">:</span> <span class="n">boost</span><span class="o">::</span><span class="n">array</span> <span class="p">{</span> <span class="c1">// ...</span> <span class="c1">// Check class size with the array size</span> <span class="n">BOOST_STATIC_ASSERT</span><span class="p">(</span> <span class="k">sizeof</span><span class="p">(</span><span class="o">*</span><span class="k">this</span><span class="p">)</span> <span class="o">==</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">T</span><span class="p">[</span><span class="n">N</span><span class="p">])</span> <span class="p">);</span> <span class="p">}</span> <span class="cp">#if BOOST_SUPPORTS_PRAGMA_PACK</span> <span class="cp">#pragma pack(pop)</span> <span class="cp">#endif</span> </pre></div></div><p> Thanx ! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3731 Trac 1.4.3 anonymous Tue, 08 Dec 2009 14:33:22 GMT component changed; owner set https://svn.boost.org/trac10/ticket/3731#comment:1 https://svn.boost.org/trac10/ticket/3731#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">No-Maintainer</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">array</span> </li> </ul> Ticket Steven Watanabe Sat, 27 Mar 2010 06:05:18 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/3731#comment:2 https://svn.boost.org/trac10/ticket/3731#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> I think it would be a bad idea to clutter the interface with such a specialized use case. If you need this, use a plain array instead of Boost.Array. </p> Ticket