GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
c++

Diagnose invalid types for bitfield widths in templates

GCC's C++ front end now diagnoses invalid non-integral operand types for bit-field widths within templates, preventing later errors or ICEs.

GCC’s C++ compiler now flags incorrect non-integral types used specified for bit-field widths inside templates at the point of template instantiation. Previously, such errors might be missed until a later stage, leading to less precise diagnostics or even internal compiler errors (ICEs) when the type was problematic. This change replicates an existing check from grokbitfield during template substitution by tsubst_decl, ensuring earlier and more consistent error reporting for invalid bit-field specifications.

In Details

This commit addresses a diagnostic gap in the C++ front end's handling of bit-field widths, specifically when they are type-dependent within templates. The original check in grokbitfield only applies when the width is not type-dependent. During template instantiation, tsubst_decl now duplicates the INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P check on TREE_TYPE(width). This ensures that non-integral bit-field width specifiers, such as floating-point types, are caught early at instantiation time, preventing later cxx_constant_value ICEs or obscure diagnostics from check_bitfield_decl.

For Context

In C++ programming, a 'bit-field' allows you to specify a variable that occupies a specific number of bits, which is useful for memory-constrained systems or low-level hardware interaction. This commit improves how the GCC C++ compiler finds errors when you define bit-fields inside 'templates'—code blueprints that work with different data types. Previously, if you tried to specify a bit-field width using a fractional number (like double) within a template, the compiler might not catch this error immediately or could even crash. Now, the compiler checks for these incorrect types earlier, during the template's specialization, providing clearer error messages and preventing crashes, making it easier to write correct C++ code.

Filed Under: c++templatesdiagnostics