4 ms·
Here is the C++11 way of specifying alignment : alignas(T) char buffer[sizeof(T)]; Or better still, just use the unrestricted union feature : union {
by keveman 14y ago
Here is the C++11 way of specifying alignment :
alignas(T) char buffer[sizeof(T)];
Or better still, just use the unrestricted union feature :
union {
T t;
};
Just use &t in place of buffer.
- quotemstr 14y agoThanks for the tip --- alignas slipped my mind. The compiler I use for most of my work doesn't yet support alignas (or unrestricted unions). I believe a VC++-specific equivalent would for the former trick would be __declspec(align(__alignof(T))).
- keveman 14y agoYeah, you are right, alignas is still unsupported in many compilers. clang 3.2 supports it. However, unrestricted unions has been supported in many compilers for a long time now. I would use that in this case, since you already know the type 'T'.