本文介绍了标准容器模板可以使用不完全类型实例化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,实例化具有不完全类型的标准容器以获得递归结构很有用:

Sometimes it's useful to instantiate a standard container with an incomplete type to obtain a recursive structure:

struct multi_tree_node { // Does work in most implementations
    std::vector< multi_tree_node > child;
};

struct trie_node { // Does not work in most implementations
    std::map< char, trie_node > next;
};

这往往工作,因为容器没有类型 value_type 或成员函数按值传递或返回任何 value_type 对象。该标准似乎没有很多关于不完整的模板参数,但在C ++ 11§17.6.4.8[lib.res.on.functions],对其他函数的要求有一点:

This tends to work because containers don't have members of type value_type or member functions that pass or return any value_type objects by value. The Standard doesn't seem to say very much about incomplete template arguments, but there is one bit under C++11 §17.6.4.8 [lib.res.on.functions], "requirements on other functions":

这是否使上述构造非法,即使实例化不在块范围?这是否属于用于实例化标准库模板组件的类型的操作(也是17.6.4.8)?

Does this make the above constructs illegal, even though the instantiations are not in block scope? Does this fall under "operations on types used to instantiate standard library template components" (also 17.6.4.8)? Or is a library implementation forbidden to incur template instantiations that might fail for incomplete types when all specifically required instantiations succeed?

编辑:由于只有函数被执行,因此只有函数可以调用和实例化其他函数,将对类型的操作...限制为块范围内的那些函数似乎将成员函数的内容保存为比签名和成员类定义的内容更严格的要求。毕竟,使用 multi_tree_node 来做任何事情肯定没有意义,直到类型完成。这扩展到 std :: unique_ptr ,它显式支持一个不完整的类型参数,即使在块范围内使用。

Since only functions may call and instantiate other functions, restricting "operations on types…" to those in block scope would seem to hold the contents of member functions to a stricter requirement than the contents of signatures and member class definitions. After all, it certainly doesn't make sense to do anything with a multi_tree_node until the type is complete. And this extends to the std::unique_ptr which explicitly supports an incomplete type argument, even when used in block scope.

编辑2:为我提供正确的方法,无需测试 trie_node 示例 - 它之前。这与@我链接的中的破损示例相同。但是,虽然文章似乎认为没有什么可以工作,解决方案似乎很简单 - std :: map 的内部 tree_node 类应该是非成员模板,而不是成员非模板类。

Edit 2: Serves me right for not bothering to test the trie_node example — and I've even tried it before. It's the same as the example of breakage in the article that @Ise linked. However, while the article seems to take for granted that "nothing like that could work," the solution seems simple to me — std::map's internal tree_node class should be a non-member template, not a member non-template class.

无论如何,

推荐答案

我个人认为,我觉得在17.6.4.8/2中的措辞实例化是一个有点
模糊,但根据

标准的意图似乎不允许使用
标准容器的递归数据类型。

Personally, I feel the wording instantiating in 17.6.4.8/2 is a littleambiguous, but according to this article,the standard's intent seems not to allow recursive data type usingstandard containers.

一个相关的注释,VC2005发出一个错误
class C {std :: deque& C> X; }; ,而它编译
class C {std :: vector< C> X; }; ...

然而,在我的理解中,这个限制只是为了扩展
实现标准容器的自由。
Kerrek SB 所提到的,可以有允许
递归数据结构的容器,以及

似乎提供此设施。

On a related note, VC2005 issues an error forclass C { std::deque< C > x; };, while it compilesclass C { std::vector< C > x; };...
However, in my understanding, this restriction is just for expanding thefreedom of the implementation of standard containers.So as Kerrek SB mentioned, there can be containers which allowrecursive data structure, andBoost.Containerseems to provide this facility.

这篇关于标准容器模板可以使用不完全类型实例化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:14