【发布时间】:2020-11-13 14:07:47
【问题描述】:
给定一个不完整的类型:
struct S;
那么下面的声明就是:
S *p; // ok, pointer to incomplete types is allowed
std::deque<S> l; // error, instantiating std::deque with incomplete type is UB
但是下面的声明呢?
std::deque<S> *p; // seems to be UB like the previous case,
// but is it ok if p is not used till S is defined?
std::deque<S*> p; // not really sure about this one
编辑:问题使用std::list 而不是std::deque,但这违背了问题的目的,因为std::list 明确allowed 使用不完整的类型。 std::deque 似乎没有这样的permission。
【问题讨论】:
-
我会说
std::list<S*> p;很好。编译器在没有布局/大小的情况下生成指向结构的指针应该不是问题。 ? -
@LanguageLawyer 不,我特意选择了
list,因为我认为它没有使用不完整类型的权限。但 eeorika 的回答表明确实如此。那我得编辑一下这个问题。 -
@LanguageLawyer 编辑了问题以使用不允许不完整类型的容器(据我所知)。
标签: c++ language-lawyer c++-standard-library incomplete-type