Forward declaration of objects with STL containers

前端 未结 3 806

Consider the following code snippet, where the first line serves only as forward declaration

 class A;

followed by defining new class

3条回答
  •  半阙折子戏
    2020-12-19 07:45

    [As supplemental instruction to Barry's answer]

    According to the standard (C++17), only std::vector, std::list and std::forward_list could be used with incomplete type when instantiating.

    §23.3.11.1/3 Class template vector overview [vector.overview]:

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

    §23.3.9.1/4 Class template forward_list overview [forwardlist.overview]:

    An incomplete type T may be used when instantiating forward_list if the allocator satisfies the allocator completeness requirements [allocator.requirements.completeness]. T shall be complete before any member of the resulting specialization of forward_list is referenced.

    §23.3.10.1/3 Class template list overview [list.overview]:

    An incomplete type T may be used when instantiating list if the allocator satisfies the allocator completeness requirements [allocator.requirements.completeness]. T shall be complete before any member of the resulting specialization of list is referenced.

提交回复
热议问题