c++ 中带有容器迭代器的循环类型依赖(GCC 失败,而 MSVC 正常)

Circular type dependency in c++ with container iterators (GCC fails, while MSVC ok)

我写的原始代码在微软的编译器中开箱即用,但不能用 gcc (4.7.4) 编译: 这是简化的代码:

// test.cpp
#include <unordered_map>
using namespace std;

struct order_rec;
typedef unordered_map<int, order_rec> placed_orders_t;
typedef placed_orders_t::iterator placed_order_iterator;
typedef unordered_map<int, placed_order_iterator> book_list_t;
typedef book_list_t::iterator book_list_iterator;
struct order_rec
{
    int a;
    book_list_iterator bi;
};

int main()
{
    book_list_t test1;
    order_rec test2;
}

g++ 不喜欢这一行:typedef placed_orders_t::iterator placed_order_iterator;,它出错是因为那一行 struct order_rec 没有完全声明。

如您所见,我有一个 int 映射 placed_orders_t => order_rec,然后我有另一个映射 book_list_t 将 int 映射到迭代器到 placed_orders_t 映射.然后,order_rec 本身将一个 interator 存储到 book_list_t 映射中。

请注意,我认为这是 gcc 实现 unordered_map(或者可能是编译器本身)中的错误:如果我将 placed_orders_t 类型定义为 std::map,那么一切都可以正常编译;但是,我确实需要在那里使用无序映射。

什么可以用作解决方法?

这是 g++ 编译的相关部分

g++-4.7 -O3 -DNDEBUG -std=c++11 -c test.cpp
...
/usr/include/c++/4.7/bits/unordered_map.h:264:11:   required from ‘class std::unordered_map<int, order_rec>’
test.cpp:7:24:   required from here
/usr/include/c++/4.7/bits/stl_pair.h:94:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
test.cpp:5:8: error: forward declaration of ‘struct order_rec’

我认为这是一个编译器问题。但通常它不应该工作,因为当你使用 typedef unordered_map<int, order_rec> placed_orders_t; 时,它需要知道 order_rec 的大小,以便它可以相应地分配内存,这在你的情况下是不知道的,所以要么使用 order_rec* 代替 order_rec 或将 struct order_rec 的定义移动到使用它之前。

您的代码是未定义的行为。当你写:

struct order_rec;
typedef unordered_map<int, order_rec> placed_orders_t;
typedef placed_orders_t::iterator placed_order_iterator;

这需要 placed_orders_t 的实例化,这需要 unordered_map<int, order_rec> 的实例化。 order_rec 此时不完整。来自 [res.on.functions]:

In particular, the effects are undefined in the following cases: [...] if an incomplete type (3.9) is used as a template argument when instantiating a template component, unless specifically allowed for that component.

标准库中的某些 class 模板允许不完整类型(如 unique_ptrvector),但 unordered_map 不是其中之一,因此其影响是不确定的。编译失败是允许的结果。请注意,即使您的代码可以编译,map 也是如此。

您必须 placed_orders_t::mapped_type 是可以在此上下文中使用的类型。也许 std::unique_ptr<order_rec>?

这不是一个很好的解决方案,但我们可以更改 order_rec 并尽早声明它。

struct  book_list_iterator;
struct order_rec
{   
    int a;
    vector    <book_list_iterator> bi;
};

这是可行的,因为 vector 明确允许不完整的类型作为成员类型。

(我确实考虑过 shared_ptrunique_ptr 而不是 vector,但我认为这是避免为 order_rec 编写复制构造函数的最简单方法)

要访问单个迭代器,我们需要 ->bi.at(0),而不仅仅是 ->bi

最后,book_list_iterator 不是 typedef,而是结构

typedef unordered_map<int, order_rec> placed_orders_t;
typedef placed_orders_t::iterator placed_order_iterator;
typedef unordered_map<int, placed_order_iterator> book_list_t;

struct  book_list_iterator : public book_list_t::iterator
{
};