Home >Backend Development >C++ >Why Can't C Containers Hold Incomplete Types?

Why Can't C Containers Hold Incomplete Types?

Linda Hamilton
Linda HamiltonOriginal
2024-12-04 07:26:10214browse

Why Can't C   Containers Hold Incomplete Types?

Why Incomplete Types Are Forbidden in C Containers

Question:

Why does the C standard disallow containers of incomplete types from being instantiated?

Answer:

The C standardization committee's decision in this regard stemmed from historical reasons.

Initial testing revealed that even simple examples of containers with incomplete types did not function consistently across all STL implementations. This lack of clarity led the committee to declare it undefined behavior for STL containers to work with incomplete types, extending this prohibition to the rest of the standard library.

This decision was taken to avoid invalidating existing library implementations that did not support incomplete types retroactively.

Despite the initial prohibition, Matt Austern, chair of the committee's library working group, acknowledged the possibility of relaxing this restriction in future C revisions.

While the prohibition still stands in the current standard, boost::container demonstrates the feasibility of containers that allow incomplete types, providing a solution for situations where it is desired.

The above is the detailed content of Why Can't C Containers Hold Incomplete Types?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn