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

标签 c++ language-lawyer c++-standard-library

在“ 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/

相关文章:

C++ 四舍五入到小数点后两位

c++ - 你能对指向另一个对象的 char* 进行算术运算吗

android - 在 android opencv 中使用 findFundamentalMat 时出错,无法解决

c++ - 如何生成用于连接字符串的可变参数宏

c++ - 未检测到-std = c++ 03编译

c++ - 访问不活动的 union 成员和未定义的行为?

c++ - 结构的初始序列是什么?

c++ - 模板参数推导失败

c++ - vector.assign() 按顺序赋值

c++ - 有什么可以一般地反转 C++ Comparator 类型吗?