title: clang templated use of __attribute__((vector_size(N)))
body: <p>I create an application which make use of a SSE4.1 vector instructions. To better manage the vector types I've created templated helper struct vector_type as follows:</p>
  
  <pre><code>template &lt;class T, int N&gt;
  struct vector_type {
     typedef T type __attribute__((vector_size(sizeof(T)*N)));
     using single_element_type = T;
     static constexpr int size = N;
     typedef union {
        type v;
        T s[N];
     } access_type;
     // some other implementation
  };
  </code></pre>
  
  <p>This compiles perfectly with g++. Unfortunately the clang++ (I use clang++ in version 3.6) complains that <code>'vector_size' attribute requires an integer constant</code>. I'm perfectly aware that this is a well known issue of clang, which is submitted as a Bug 16986. My question is rather is there a way to workaround the problem. I came up with the code:</p>
  
  <pre><code>template &lt;class T, int ASize&gt;
  struct vector_type_impl;
  
  template &lt;&gt;
  struct vector_type_impl&lt;int,16&gt; {
     typedef int type __attribute__((vector_size(16)));
  };
  
  template &lt;class T, int N&gt;
  struct vector_type: vector_type_impl&lt;T, N*sizeof(T)&gt; {
     using type = typename vector_type_impl&lt;T, N*sizeof(T)&gt;::type;
     using single_element_type = T;
     static constexpr int size = N;
     typedef union {
        type v;
        T s[N];
     } access_type;
     // some other implementation
  };
  </code></pre>
  
  <p>But I can't believe there is no better way to do it.</p>