Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Imagine</p> <pre><code>#define CACHE_LINE_SIZE 32 sizeof(T) == 48 </code></pre> <p>Now, consider how <code>[[ align(CACHE_LINE_SIZE) ]]</code>, works. eg:</p> <pre><code>[[ align(32) ]] Foo foo; </code></pre> <p>This will force <code>sizeof(Foo) == 32n</code> for some <code>n</code>. ie align() will pad for you, if necessary, in order for things like <code>Foo foo[10];</code> to have each <code>foo[i]</code> aligned as requested.</p> <p>So, in our case, with <code>sizeof(T) == 48</code>, this means <code>sizeof(cache_line_storage&lt;T&gt;) == 64</code>.</p> <p>So the alignment gives you the padding you were hoping for.</p> <p>However, this is one 'error' in the template. Consider this case:</p> <pre><code>#define CACHE_LINE_SIZE 32 sizeof(T) == 32 </code></pre> <p>Here we end up with <code>char pad[1];</code>. Which means <code>sizeof(cache_line_storage&lt;T&gt;) == 64</code>. Probably not what you want!</p> <p>I think the template would need to be modified somewhat:</p> <pre><code>template &lt;typename T, int padding&gt; struct pad_or_not { T data; char pad[padding]; }; // specialize the 0 case // As it is late, I am SURE I've got the specialization syntax wrong... template &lt;typename T, int&gt; struct pad_or_not&lt;0&gt; { T data; }; template&lt;typename T&gt; struct cache_line_storage { [[ align(CACHE_LINE_SIZE) ]] pad_or_not&lt;T, (sizeof(T) &gt; CACHE_LINE_SIZE ? 0 : CACHE_LINE_SIZE - sizeof(T) ) &gt; data; }; </code></pre> <p>or something like that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload