【问题标题】:template error: nontype ".. [with T=T] is not a type name"模板错误:非类型“.. [with T=T] 不是类型名称”
【发布时间】:2012-08-11 18:07:35
【问题描述】:

尝试对我的内存对齐进行 typedef 我得出了以下构造(由于我需要更正 GNU 版本,这仍有一些工作正在进行中):

#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  

然后我尝试像这样使用它:

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

但随后我收到以下错误消息:

/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;
 ^

谁能告诉我这里出了什么问题?

【问题讨论】:

    标签: c++ templates compiler-errors cross-compiling


    【解决方案1】:

    aptr32 依赖于T 所以:

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

    更多关于typename使用的解释请看Where and why do I have to put the "template" and "typename" keywords?

    【讨论】:

    • 很好,谢谢!我实际上对此感到困惑......为什么数据成员必须在前面有一个 typedef 标记才能编译?这有点奇怪。
    • @GiovanniAzua,链接的答案解释了 typename 的使用比我做得更好。
    • @GiovanniAzua:不是数据成员,但它的类型以typedef为前缀。
    • 简短的回答是,即使编译器可能有您对sfo_type 的定义,但在您使用它的时候可能会有一个明确的特化(如 sfo_type);不要求特化具有与通用模板相同的成员,因此您必须告诉编译器“此名称必须是类型的名称”。这就是typename 所做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    相关资源
    最近更新 更多