C containers famously prohibit the instantiation with incomplete types. Boost's container library circumvents this restriction, raising questions about its purpose and potential drawbacks.
Rationale for the Restriction
Historically, the C standardization committee found varying implementation behaviors with incomplete types across different STL implementations. To ensure compatibility and avoid confusion, they explicitly prohibited incomplete types in standard library containers.
Potential Downsides of Allowing Incomplete Types
While allowing incomplete types might enable advanced data structures, it could also pose runtime issues. Incomplete types lack necessary information, making it difficult for containers to manage memory allocation and object lifetime.
Future Possibilities
The C committee has acknowledged the potential benefits of allowing incomplete types. In 2002, the chair of the library working group hinted that this restriction might be relaxed in future versions of C . However, as of the current standard, the prohibition remains in place.
Boost's Solution
Recognizing the limitations of the standard's approach, Boost developers created their own containers capable of working with incomplete types. This enabled the creation of recursive data structures that were otherwise impossible with the standard library.
C 17 Partial Allowance
C 17 introduced a partial relaxation of this restriction for certain containers, allowing the use of incomplete types for types that are forward-declared in the container's element type or key type. This limited allowance improves compatibility with some modern programming patterns while maintaining stability with existing implementations.
The above is the detailed content of Why Can't C Standard Containers Use Incomplete Types, and What Are the Workarounds?. For more information, please follow other related articles on the PHP Chinese website!