Title
Allowing expansion over non-constant std::array
Status
open
Section
8.7 [stmt.expand]
Submitter
Barry Revzin

Created on 2025-12-04.00:00:00 last changed 7 days ago

Messages

Date: 2025-12-05.17:46:39

Possible resolution:

  1. Change in 6.8.7 [class.temporary] paragraph 9 (as modified by issue 3043) as follows:

    The sixth context is when a temporary object is created in the expansion-initializer of an iterating or a destructuring expansion statement. If such a temporary object would otherwise be destroyed at the end of that expansion-initializer, the object persists for the lifetime of the reference initialized by the expansion-initializer, if any.
  2. Change in 8.7 [stmt.expand] bullet 5.2 as follows:

    
    
    
    • ...
    • Otherwise, if S is an iterating expansion statement, S is equivalent to:
        {
          init-statement
          constexpropt auto&& range = expansion-initializer ;
          constexpropt auto begin = begin-expr;  // see 8.6.5 [stmt.ranged]
          constexpr auto end = end-expr;      // see 8.6.5 [stmt.ranged]
          S0
          .
          .
          .
          SN-1
        }
      
      where N is the result of evaluating the expression
        [&] consteval {
          std::ptrdiff_t result = 0;
          for (auto i = begin ; i != end ; ++i, ++result);
          auto b = begin-expr ;
          auto e = end-expr ;
          for (; b != e; ++b) ++result;
          return result;  // distance from begin to end
        }()
      
      and Si is
        {
          constexpropt auto iter = begin + i;
          for-range-declaration = *iter ;
          compound-statement
        }
      
      The variables range, begin, end, and iter are defined for exposition only. The keyword constexpr is present in the declaration of range and begin if and only if constexpr is one of the decl-specifiers of the decl-specifier-seq of the for-range-declaration. [Note 1: The instantiation is ill-formed if range is not a constant expression (7.7 [expr.const]). —end note]
    • ...
Date: 2025-12-04.00:00:00

(From submission #805.)

(Split off from issue 3131.)

In order to allow expansion over non-constant std::array (which does have a compile-time size), the constexpr in the range declaration in 8.7 [stmt.expand] bullet 5.2 should be optional, similar to the destructuring case.

History
Date User Action Args
2025-12-04 23:31:25adminsetmessages: + msg8430
2025-12-04 00:00:00admincreate