9

Trying to typedef my memory alignment I came out with the following construct (which still is a bit of work in progress because I need to correct the GNU version):

#if defined(__GNUG__)
template <typename T>
struct sfo_type {
    typedef T* restrict __attribute__((aligned(32))) aptr32;
};

#elif defined(__INTEL_COMPILER)
template <typename T>
struct sfo_type {
    typedef T* restrict __attribute__((aligned(32))) aptr32;
};
#endif  

and then I try to use it like this:

template<typename T>
class tsfo_vector {
private:
   sfo_type<T>::aptr32  m_data;
   int                  m_size;
...

but then I get the following error message:

/Users/bravegag/code/fastcode_project/code/src/sfo_vector.h(43): error: nontype "sfo_type<T>::aptr32 [with T=T]" is not a type name
 sfo_type<T>::aptr32 m_data;
 ^

Can anyone advice what's wrong here?

hmjd
  • 120,187
  • 20
  • 207
  • 252
SkyWalker
  • 13,729
  • 18
  • 91
  • 187

1 Answers1

13

aptr32 is dependent on T so:

template<typename T>
    class tsfo_vector {
    private:
        typename sfo_type<T>::aptr32 m_data;
      //^^^^^^^^

For further explanation on the use of typename see Where and why do I have to put the "template" and "typename" keywords?

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Nice, thank you! I am actually puzzled by this one ... how come a data member has to have a typedef marker in front to compile? it is a bit of a weird thing. – SkyWalker Aug 15 '12 at 12:45
  • @GiovanniAzua, the answer linked explains the use of `typename` much better than I could. – hmjd Aug 15 '12 at 12:49
  • @GiovanniAzua: Not the data member, but its type is prefixed with `typedef`. – celtschk Aug 15 '12 at 12:53
  • 1
    The short answer is that even though the compiler may have your definition of `sfo_type`, it's possible that at the point where you use it there will be an explicit specialization (like sfo_type); there is no requirement that specializations have the same members as the general template, so you have to tell the compiler "this name **must be** the name of a type". That's what `typename` does. – Pete Becker Aug 15 '12 at 15:37