gpt4 book ai didi

c++ - C++标准是否规定了标准容器的类型依赖?

转载 作者:太空狗 更新时间:2023-10-29 20:01:36 24 4
gpt4 key购买 nike

在“ 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_liststd::liststd: :vector 可以用不完整的类型实例化。例如

[vector.overview]/3 An incomplete type T may be used when instantiating vector if the allocator satisfies the allocator completeness requirements (20.5.3.5.1). T shall be complete before any member of the resulting specialization of vector is referenced.

forward_listlist 有相似的措辞。

关于c++ - C++标准是否规定了标准容器的类型依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56692892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com