Error "requested alignment is not an integer constant"
<p>I'm having trouble working around a GCC issue. I'm experiencing it under GCC 4.8, but not 5.1. It looks like its been reported <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56859" rel="nofollow noreferrer">here</a> and/or <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53017" rel="nofollow noreferrer">here</a>.</p>

<p>The issue surfaces as follows:</p>

<pre><code>template &lt;bool B&gt;
struct S
{
    static const int ALIGN = 16;
    __attribute__((aligned(ALIGN))) int x;
};

int main(int argc, char* argv[])
{
    S&lt;true&gt; s1;
    S&lt;false&gt; s2;
    return 0;
}
</code></pre>

<p>And:</p>

<pre><code>$ g++ test.cxx -o test.exe
test.cxx:9:41: error: requested alignment is not an integer constant
     __attribute__((aligned(ALIGN))) int x;
</code></pre>

<p>Its also kind of important that I keep the <code>static const</code> because Clang does not optimize as well as GCC. And C++03 is a requirement as well.</p>

<p>Here's a related question, but it just identifies the bug, and does not offer a workaround: <a href="https://stackoverflow.com/q/34376921">Using constant from template base class</a>.</p>

<p>What can I do to work around the issue?</p>

<hr>

<p>Here's the problem compiler, but there are probably others out there considering one of the issues remained for 3 years or so.</p>

<pre><code>$ g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
</code></pre>

<hr>

<p>The actual use case is a little more involved. If the machine offers SSE2 or above, then alignment is 16. If the machine offers SSE4 or above, then the then alignment is 32. Otherwise, we fall back to natural alignment. So its kind of closer to:</p>

<pre><code>template &lt;class W, bool B, unsigned int S&gt;
struct X
{
    static const int ALIGN = (B ? 16 : sizeof(W));
    __attribute__((aligned(ALIGN))) W x[S];
};

int main(int argc, char* argv[])
{
    X&lt;int, true, 10&gt; x1;
    X&lt;long, false, 20&gt; x2;
    return 0;
}
</code></pre>