Standard Container Templates and Incomplete Types
The question arises whether standard container templates can be instantiated with incomplete types. While certain container constructions may appear to work in practice, the C 11 Standard explicitly prohibits this under §17.6.4.8 [lib.res.on.functions].
The Standard states that, "… if an incomplete type (3.9) is used as a template argument when instantiating a template component, unless specifically allowed for that component." This restriction applies to member functions, member class definitions, and signatures, but not to operations within block scope.
The standard library implementation cannot induce template instantiations that would fail for incomplete types, even if other specifically required instantiations succeed. This prohibition is intended to prevent potential issues, such as self-referential types arising from optimizations like "small vectors."
Exceptions exist, such as the std::unique_ptr class template, which explicitly supports incomplete type arguments, even within block scope. However, for standard containers, the restriction on using incomplete types remains in place, ensuring consistent behavior across implementations.
The above is the detailed content of Can Standard Container Templates be Instantiated with Incomplete Types?. For more information, please follow other related articles on the PHP Chinese website!