C++标准是否规定了标准容器的类型依赖?
Does the C++ standard specify the type dependencies of standard containers?
在“ ”中观察到
std::unordered_map<Key, Value>
对 libstdc++ 中的 Value
具有类型依赖性(这是意外的)并且对 libc++ 和 MSVC 中的 Value
没有类型依赖性。
一般来说,ISO C++ 规范是否讨论容器的类型依赖性?如果可以,您能指出相关部分吗?
类型依赖:我不确定 ISO C++ 规范中是否有类型依赖的正式定义,但出于本文的目的 post,假设 A 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 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_list
和 list
的措辞相似。
在“std::unordered_map<Key, Value>
对 libstdc++ 中的 Value
具有类型依赖性(这是意外的)并且对 libc++ 和 MSVC 中的 Value
没有类型依赖性。
一般来说,ISO C++ 规范是否讨论容器的类型依赖性?如果可以,您能指出相关部分吗?
类型依赖:我不确定 ISO C++ 规范中是否有类型依赖的正式定义,但出于本文的目的 post,假设 A 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
的措辞相似。