Custom alignment is not part of the standard, so how compilers deal with it depends on them - it seems that VC ++ does not like to combine templates with __declspec.
I offer a job using specialization, 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 infer from this code:
template<int Align> class MyClass { private: struct MyStruct : aligned<Align> {
This, unfortunately, violates the POD version of MyStruct. It also does not work with inline / existing types, so you have to use a wrapper for them.
aligned_t<int, 4> myAlignedVariable;
pmdj source share