This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-11-11


652. Compile-time evaluation of floating-point expressions

Section: 7.7  [expr.const]     Status: CD2     Submitter: Jens Maurer     Date: 3 October 2007

[Voted into the WP at the March, 2009 meeting.]

It was the intention of the constexpr proposal that implementations be required to evaluate floating-point expressions at compile time. This intention is not reflected in the actual wording of 7.7 [expr.const] paragraph 2, bullet 5:

This restriction has the effect of forbidding the use of floating-point expressions in integral constant expressions.

Proposed resolution (June, 2008):

Delete bullet 6 of 7.7 [expr.const] paragraph 2:

Notes from the June, 2008 meeting:

The CWG agreed with the intent of this issue, that floating-point calculations should be permitted in constant expressions, but acknowledged that this opens the possibility of differing results between compile time and run time. Such issues should be addressed non-normatively, e.g., via a “recommended practice” note like that of C99's 6.4.4.2 or in a technical report.

Proposed resolution (August, 2008):

  1. Delete bullet 6 of 7.7 [expr.const] paragraph 2:

  2. Add a new paragraph after 7.7 [expr.const] paragraph 3:

  3. [Note: Although in some contexts constant expressions must be evaluated during program translation, others may be evaluated during program execution. Since this International Standard imposes no restrictions on the accuracy of floating-point operations, it is unspecified whether the evaluation of a floating-point expression during translation yields the same result as the evaluation of the same expression (or the same operations on the same values) during program execution. [Footnote: Nonetheless, implementations are encouraged to provide consistent results, irrespective of whether the evaluation was actually performed during translation or during program execution. —end footnote] [Example:

      bool f() {
        char array[1 + int(1 + 0.2 - 0.1 - 0.1)];  // Must be evaluated during translation
        int size = 1 + int(1 + 0.2 - 0.1 - 0.1);   // May be evaluated at runtime
        return sizeof(array) == size;
      }
    

    It is unspecified whether the value of f() will be true or false. —end example] —end note]