在“ Codependent types with unordered_map ”中观察到
std::unordered_map<Key, Value>
对 Value
具有类型依赖性在 libstdc++ 中(这是意外的)并且对 Value
没有类型依赖性在 libc++ 和 MSVC 中。
一般来说,ISO C++ 规范是否完全讨论了容器的类型依赖性?如果可以,您能否指出相关部分?
类型依赖:我不确定 ISO C++ 规范中是否有类型依赖的正式定义,但为了本文的目的,我们假设一个 type A
类型依赖于 type B
如果 A 不能单独使用 B 的前向声明进行编译。示例:
struct Val; // forward declaration of Val
struct Container {
Val v;
}; // Compile error; Type Val is incomplete. Container has a type dependency on Val
struct Val; // forward declaration of Val
struct Container2 {
Val *v;
}; // Compiles. Container2 does not have type dependency on Val
最佳答案
您可能正在寻找这个:
[res.on.functions]/2 In particular, the effects are undefined in the following cases:
...
(2.5) — if an incomplete type (6.9) is used as a template argument when instantiating a template component, unless specifically allowed for that component.
在各种标准容器中,[containers] 部分指定了 std::forward_list
、std::list
和 std: :vector
可以用不完整的类型实例化。例如
[vector.overview]/3 An incomplete type
T
may be used when instantiatingvector
if the allocator satisfies the allocator completeness requirements (20.5.3.5.1).T
shall be complete before any member of the resulting specialization ofvector
is referenced.
forward_list
和 list
有相似的措辞。
关于c++ - C++标准是否规定了标准容器的类型依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56692892/