嵌套模板类的前向声明

Forward declaration of nested template classes

本文关键字:声明 嵌套      更新时间:2023-10-16

我有一个类似的类:

template< typename T, typename Allocator >
class MemMngList : protected LockFreeMemMng<Node, Link>
{
public:
    typedef T Type;
    typedef Allocator AllocatorType;
    struct Node : public LockFreeNode
    {
    public:
        struct Link : protected LockFreeLink< Node >
        {
                    ....

问题是我在 LockFreeMemMng('Node' : 未声明的标识符...)的模板参数中出现错误。

如何在上述 MemMngList 实现中使用 NodeLink的前向声明?

template< typename T, typename Allocator >
class MemMngList;
//Forward Declaration of Node and Link

不能在类声明中转发声明某些内容。如果您想访问私有成员,您需要将其移动到类外的某个位置并使用 friend:

template <typename T, typename Allocator>
struct NodeType : public LockFreeNode< NodeType<T,Allocator> >
{
    ...
    template <typename,typename>
    friend class MemMngList;
};
template <typename T, typename Allocator>
struct LinkType : public LockFreeLink< NodeType <T,Allocator> >
{
    ...
    template <typename,typename>
    friend class MemMngList;
};
template< typename T, typename Allocator >
class MemMngList : protected LockFreeMemMng< NodeType <T,Allocator> , LinkType <T,Allocator> >
{
    typedef NodeType <T,Allocator> Node;
    typedef LinkType <T,Allocator> Link;
    ...
};