template error: nontype “.. [with T=T] is not a ty

2019-06-17 02:01发布

问题:

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?

回答1:

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?