Home > Backend Development > C++ > body text

Can Standard Container Templates be Instantiated with Incomplete Types?

Barbara Streisand
Release: 2024-11-04 14:59:40
Original
936 people have browsed it

Can Standard Container Templates be Instantiated with Incomplete Types?

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!