https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113571

            Bug ID: 113571
           Summary: Preprocessor if directive does not correctly recognize
                    all C++ integral constant expressions
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luigighiron at gmail dot com
  Target Milestone: ---

When preprocessing C++ code, GCC interprets preprocessor if expressions using
the same rules as C constant expressions. For example the following
preprocessor if directives cause errors when they should be valid C++:

#if(1,2)
#endif

#if 0?1.,2:3
#endif

#if([]{}(),1)==1
#endif

#if 0<=>0==0
#endif

The C++ standard requires that the expression in a preprocessor if directive is
an integer constant expression:

> The expression that controls conditional inclusion shall be an integral
> constant expression except that identifiers (including those lexically
> identical to keywords) are interpreted as described below and it may contain
> zero or more defined-macro-expressions and/or has-include-expressions and/or
> has-attribute-expressions as unary operator expressions.
Section 15.2 "Conditional inclusion" [cpp.cond] Paragraph 1 ISO/IEC 14882:2020

> An integral constant expression is an expression of integral or unscoped
> enumeration type, implicitly converted to a prvalue, where the converted
> expression is a core constant expression.
Section 7.7 "Constant expressions" [expr.const] Paragraph 8 ISO/IEC 14882:2020

The way C++ describes preprocessor if expressions is equivalent to constant
expressions outside of the preprocessor, so all of the examples listed in this
bug report should be valid (GCC does correctly allow these expressions in other
contexts, such as the value of an enumerator).

These quotes were taken from N4868, so the paragraph numbers not might match
exactly. The text in drafts after C++20 contain the same wording so they should
be correct.

Reply via email to