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 🙂
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:
and then derive from that in your code:
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.