Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 另一个奇怪的重复模板模式 模板 类节点:公共数据{ // ... };_C++_Templates_F Bounded Polymorphism - Fatal编程技术网

C++ 另一个奇怪的重复模板模式 模板 类节点:公共数据{ // ... };

C++ 另一个奇怪的重复模板模式 模板 类节点:公共数据{ // ... };,c++,templates,f-bounded-polymorphism,C++,Templates,F Bounded Polymorphism,问题很简单,如何使上述代码编译? 这样做的目的是给节点分配其他节点的可能性(并提供默认分配器)。您不能这样写: template <class Data, class Allocator = std::allocator<Node> > class Node : public Data { // ... }; 模板 类节点; 这个怎么样 template<typename Data, typename Allocator = std::allocator<

问题很简单,如何使上述代码编译?
这样做的目的是给节点分配其他节点的可能性(并提供默认分配器)。

您不能这样写:

template <class Data, class Allocator = std::allocator<Node> >
class Node : public Data {
  // ...
};
模板
类节点;
这个怎么样

template<typename Data, typename Allocator = std::allocator<Data> >
struct Container {
  struct Node : Data { 
    typedef typename Allocator::template rebind<Node>::other NodeAllocator;
    ...
  };
  ...
};
#包括
模板
类NodeImpl:公共数据
{
};
模板>
类节点:public NodeImpl
{
};
类MyAllocator
{
};
类MyDataClass
{
};
int main()
{
节点;
Node Node_与_alloc;
返回0;
}

您无法将其编译-您试图创建的是“无限”类型

首先,不能将未实例化的类模板用作模板参数。所以您需要将节点传递给std::allocator,如下所示:

#include <memory>

template<class Data>
class NodeImpl : public Data
{
};

template<class Data, class Allocator = std::allocator< NodeImpl<Data> > >
class Node : public NodeImpl<Data>
{
};

class MyAllocator
{
};

class MyDataClass
{
};

int main()
{
    Node<MyDataClass> node;

    Node<MyDataClass, MyAllocator> node_with_alloc;

    return 0;
}
template <class Data, class Allocator = std::allocator<Data> > class Node ...
模板
类节点。。。
然而,那会是什么呢?嗯,std::分配器 诀窍在于,分配器不仅需要分配其模板参数,还需要分配任何其他类型的参数。宣布你的班级为

template <class Data, class Allocator = std::allocator<Node<Data, Something> > > 
class Node ...
模板类节点。。。
然后,为如下节点创建分配器:

#include <memory>

template<class Data>
class NodeImpl : public Data
{
};

template<class Data, class Allocator = std::allocator< NodeImpl<Data> > >
class Node : public NodeImpl<Data>
{
};

class MyAllocator
{
};

class MyDataClass
{
};

int main()
{
    Node<MyDataClass> node;

    Node<MyDataClass, MyAllocator> node_with_alloc;

    return 0;
}
template <class Data, class Allocator = std::allocator<Data> > class Node ...
typename分配器::重新绑定::其他节点定位器(myDataAllocator)

可能会有帮助,尽管它过于关注迭代器

我终于解决了!解决方案是将默认分配器的专门化延迟到已定义节点的类内部:

typename Allocator::rebind<Node>::other nodeAllocator(myDataAllocator)
模板
类节点:公共数据{
typedef计算器分配器;
// ...
};

另一种解决方案。这一个似乎更为典型。向量和智能指针的实现使用了类似的东西。其想法是从分配器中私下继承:

template <class Data, template<class T> class TAllocator = std::allocator >
class Node : public Data {
  typedef TAllocator<Node> Allocator;
  // ...
};
模板
类节点:公共数据、私有分配器{
// ...
};

好处是,在继承声明中,我们已经可以使用节点。

不是真正的解决方案,节点仍然无法分配。他也可以使用模板,本质上是一样的。
template <class Data, template <class N> class Allocator = std::allocator>
class Node : public Data, private Allocator<Node<Data, Allocator> > {
  // ...
};