2

I am writing a software rasteriser using MSVC++ Express 2010 for windows. I am using SSE and need aligned data structures. I have a number of separate vector structs for different fundamental data types (float, int etc.) that I want to roll into one templated struct for convenience. The _declspec(align(16)) tag which has served well for aligning structs doesn't appear to work for templates. What are my options? This is what I would like to achieve:

/* _declspec(align(16)) */
template< typename T > struct baseVector 
{
    T   v[ 4 ];
};

typedef baseVector< float > vector, vertex, point;  // etc

I have tried specialising the template and using the _declspec(align()) but the compiler complains. I have tried using #pragma pack() as well but I don't think that has any aligning effect when these structs are members of higher level structs.

lamorna
  • 23
  • 3

1 Answers1

5

The declspec is in the wrong place. It should be after the struct.

template<typename T> 
struct _declspec(align(16)) baseVector 
{
    T v[4];
};
Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71
  • according to MSDN, it can be immediately before as well: http://msdn.microsoft.com/en-us/library/83ythb65.aspx – Necrolis Feb 27 '12 at 07:06
  • It appears to have no effect. My alignment errors continue. @Necrolis: putting it before the struct keyword yields this error: error C2988: unrecognizable template declaration/definition. – lamorna Feb 28 '12 at 04:01
  • @lamorna: guess it falls back to another case of MSDN having bad documentation... – Necrolis Feb 28 '12 at 07:51
  • After further investigation I found my problem was a subtle pre-processor bug and not an alignment issue. The `declspec` does work with templates. Thank you. – lamorna Feb 29 '12 at 07:44