Search code examples
c++templates

Aligning Member Variables By Template Type


I want to align my member variables based on a class template type but I'm not sure if it is actually possible.

The following is a (very) simple example of what I'd like to do

template<int Align>
class MyClass
{
private:
  struct MyStruct
  {
    // Some stuff
  } __declspec(align(Align));

  __declspec(align(Align)) int myAlignedVariable;
};

So what I'd like is for Align to be a per-instance variable, and only through that is the align value of the class contents decided.

Unfortunately I always get the following error

error C2975: 'test::MyClass' : invalid template argument for 'Align', expected compile-time constant expression

So, is this actually possible or can the alignment only be possible using a fixed compile time constant? If not, can anyone think of a way around this?

Thanks :)


Solution

  • Custom alignment isn't in the standard, so how the compilers deal with it is up to them - looks like VC++ doesn't like combining templates with __declspec.

    I suggest a work-around using specialisation, something like this:

    template<int A> struct aligned;
    template<> struct aligned<1> { } __declspec(align(1));
    template<> struct aligned<2> { } __declspec(align(2));
    template<> struct aligned<4> { } __declspec(align(4));
    template<> struct aligned<8> { } __declspec(align(8));
    template<> struct aligned<16> { } __declspec(align(16));
    template<> struct aligned<32> { } __declspec(align(32));
    

    and then derive from that in your code:

    template<int Align>
    class MyClass
    {
    private:
      struct MyStruct : aligned<Align> {
        // stuff
      };
    };
    

    This unfortunately breaks the POD-ness of MyStruct. It also doesn't work with built-in/existing types, so you'll have to use a wrapper for those.

    aligned_t<int, 4> myAlignedVariable;