Implement C++ attribute to prevent template specializations and new warning.
C++ compiler now supports `[[clang::no_specializations]]` attribute and `-Winvalid-specialization` warning.
GCC now supports the [[clang::no_specializations]] attribute for class, variable, and function templates. Applying this attribute prevents any partial or full specialization of the template, issuing an error (which can be warned with -Winvalid-specialization). This feature is primarily intended for libraries like libstdc++ to help diagnose issues in standard library template usage and consistency.
In Details
The C++ frontend now parses and implements the [[clang::no_specializations]] attribute, which applies to template declarations and prevents their instantiation via partial or full specialization. This change affects gcc/cp/decl2.cc and gcc/cp/parser.cc, ensuring that attempts to specialize such templates result in an error, configurable as a warning via -Winvalid-specialization. This aligns GCC's behavior with Clang's for this attribute, primarily for use in standard library development.
- template specialization
- A mechanism in C++ that allows a specific version of a template (class, function, or variable) to be defined for a particular set of template arguments. Partial specialization applies to class templates, while full specialization can apply to all.
- attribute
- A C++ language feature that allows annotations to be attached to declarations (like classes, functions, variables) to provide additional information or directives to the compiler. Examples include
[[noreturn]]or[[deprecated]]. - libstdc++
- The standard C++ library implementation used by GCC. It provides essential components like containers, algorithms, and I/O streams.