包含自身图的C ++结构 [英] C++ Struct that contains a map of itself

查看:45
本文介绍了包含自身图的C ++结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题:如何使它正常工作?

Simple question: How do I get this to work?

struct A {
    double whatever; 
    std::unordered_map<std::string, A> mapToMoreA; 
}

g ++错误:std :: pair< _T1,_T2> :: second的类型不完整

g++ error: std::pair<_T1, _T2>::second has incomplete type

据我了解,在实例化映射时,编译器需要知道A的大小,但是它不知道,因为映射是在A的声明中声明的,因此这是解决此问题的唯一方法使用指向A的指针(不想这样做)?

As far as I understand, when instantiating the map, the compiler needs to know the size of A, but it doesn't know this because the map is declared in A's declaration, so is the only way to get around this to use pointers to A (don't feel like doing that)?

推荐答案

在大多数情况下,它取决于容器实现的详细信息(更确切地说,取决于在容器声明时实例化的内容以及未实例化的内容).显然, std :: unordered_map 实现要求类型完整.同时,GCC的 std :: map 实现完全可以使用不完整的类型进行编译.

Most of the time it will depend on the container implementation details (more precisely, on what gets instantiated at the point of container declaration and what doesn't). Apparently, std::unordered_map implementation requires the types to be complete. At the same time GCC's implementation of std::map compiles perfectly fine with incomplete type.

为说明这种差异的根源,请考虑以下示例.假设我们决定对 std :: vector 类似的功能进行自己的幼稚实现,并声明我们的vector类如下

To illustrate the source of such difference, consider the following example. Let's say we decided to make our own naive implementation of std::vector-like functionality and declared our vector class as follows

template <typename T> class my_vector {
  T *begin;
  T *end;
  ...
};

只要我们的类定义仅包含指向 T 的指针,对于类定义本身,就不需要完整的 T 类型.我们可以实例化 my_vector 本身以获得完整的 T ,而不会出现任何问题

As long as our class definition contains only pointers to T, the type T is not required to be complete for the class definition itself. We can instantiate my_vector itself for an incomplete T without any problems

class X;
my_vector<X> v; // OK

以后,当我们开始使用(并实例化) my_vector 的各个方法时,将需要类型的完整性".

The "completeness" of the type would be required later, when we begin to use (and therefore instantiate) the individual methods of my_vector.

但是,如果由于某种原因我们决定将 T 的直接实例包含到我们的向量类中,事情将会发生变化

However, if for some reason we decide to include a direct instance of T into our vector class, things will chahge

template <typename T>
class my_vector {
  T *begin;
  T *end;
  T dummy_element;
  ...
};

现在,在 my_vector 本身实例化时,很早就需要 T 的完整性

Now the completeness of T will be required very early, at the point of instantiation of my_vector itself

class X;
my_vector<X> v; // ERROR, incomplete type

这种情况一定在您的情况下发生.您正在处理的 unordered_map 的定义包含 A 的直接实例.这就是无法实例化的原因(显然,在这种情况下,您将最终获得无限递归类型).

Something like that must be happening in your case. The definition of unordered_map you are dealing with somehow contains a direct instance of A. Which is the reason why it is impossible to instantiate (obviously, you would end up with infinitely recursive type in that case).

更好地考虑通过实现 unordered_map 可以确保不将 A 作为直接成员包含在自身中.这样的实现不需要 A 即可完成.正如您自己指出的那样,在这方面,Boost的 unordered_map 的实现设计得更好.

A better thought through implementation of unordered_map would make sure not to include A into itself as a direct member. Such implementation would not require A to be complete. As you noted yourself, Boost's implementation of unordered_map is designed better in this regard.

这篇关于包含自身图的C ++结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆