我是否声明了部分专门化的友类?-非常困惑

Am I declaring partially specialized friend class? - very confused

本文关键字:非常 声明 是否 专门化      更新时间:2023-10-16

我已经在这个问题上花了太多时间了。我正在尝试使用两个不同的分配器为节点和它们指向的类型实现单个链表。下面的代码一直抱怨我在SingleListNode定义中部分专门化了友类声明:

namespace containers {
template<typename T, typename TAlloc,
typename NAlloc>class SingleList; // forward declaration
template<typename T, typename TAlloc = std::allocator<T>>
class SingleListNode {
    template<typename T1, typename T2, typename T3>
    friend class SingleList<T1, T2, T3> ; // partially specialized???
    // class definition
};
template<typename T, typename TAlloc = std::allocator<T>,
        typename NAlloc = std::allocator<SingleListNode<T>>>
class SingleList {
    // class definition
};
} // end of namespace containers

一直在告诉我:

./src/SingleList .h:21:16:错误:' template struct containers::SingleList '的特化必须出现在命名空间作用域中/src/singllist .h:21:39:错误:partial特殊化' containers:: singllist '声明为' friend '

据我所知,这不是专门化。也许这是GCC编译器中的一个bug ?否则,我哪里出错了?

您正在声明一个模板化的友类,因此正确的语法是

 template<typename T1, typename T2, typename T3> 
 friend class SingleList;

SingleList后面没有<T1, T2, T3>。例如,这里的"实际使用示例"