GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
c++

C++: Fix parser acceptance of stray ':' in init-statements

GCC's C++ parser now correctly rejects stray colons following 'using' declarations in init-statements.

GCC’s C++ parser previously accepted an incorrect syntax in initialization statements, specifically a stray colon after a ‘using’ declaration, by mistaking it for a range-based for loop syntax. This commit corrects the parser to properly handle such cases, ensuring adherence to the C++23 standard and providing more accurate error reporting by pointing to the ‘using’ keyword instead of the erroneous colon.

In Details

The cp_parser_init_statement in gcc/cp/parser.cc was modified to fix a C++23 pedwarn related to using declarations followed by a colon within an init-statement. The parser was previously fooled by the colon, thinking it was a range-for loop. The fix ensures that the colon is only consumed if expect_semicolon_p is true, and the location reported for the error now points to the using keyword.

For Context
init-statement
A statement, typically used in if or switch constructs in C++ (since C++11), that declares and initializes a variable within the statement itself. For example, if (int x = get_value()) { ... }.
using declaration
In C++, a using declaration (e.g., using T = int;) introduces an alias or brings names into the current scope. In C++20 and later, using declarations can appear in init-statements.
range-based for loop
A C++ feature (introduced in C++11) that provides a simpler syntax for iterating over elements in a range or container. It has the form for (element : range) { ... }.
pedwarn
A warning that flags constructs that are technically valid according to the C++ standard but are considered non-standard, obsolete, or potentially problematic (pedantic warning).
Filed Under: c++parsersyntax