|
1 | 1 | Version 7.1 |
2 | 2 |
|
| 3 | +11/12/26 [GH #368] |
| 4 | +C++26: Allowing exception throwing in constant evaluation |
| 5 | + |
| 6 | +P3068R6 is now implemented in C++26 mode, except for its library support: |
| 7 | +Exceptions may be thrown and caught during constant evaluation, provided that |
| 8 | +every exception thrown is also caught (and its exception object destroyed) |
| 9 | +within the same evaluation. For example, with --c++26: |
| 10 | + |
| 11 | + constexpr int f(int n) { |
| 12 | + try { |
| 13 | + if (n < 0) throw n; |
| 14 | + return n; |
| 15 | + } catch (int i) { |
| 16 | + return -i; |
| 17 | + } |
| 18 | + } |
| 19 | + static_assert(f(-3) == 3); // Previously an error; now okay. |
| 20 | + |
| 21 | +The feature can be enabled in other modes with |
| 22 | +--set_flag=constexpr_exceptions, and disabled with |
| 23 | +--clear_flag=constexpr_exceptions. It is always disabled if exceptions are |
| 24 | +disabled. |
| 25 | + |
| 26 | +An exception that is not caught, a rethrow when no exception is being |
| 27 | +handled, and a call to std::terminate (e.g., because an exception escapes from |
| 28 | +a noexcept function) all cause the evaluation to not be constant, with a |
| 29 | +specific diagnostic. |
| 30 | + |
| 31 | + |
| 32 | +11/12/26 [GH #368] |
| 33 | +C++26: Library support for exceptions in constant evaluation |
| 34 | + |
| 35 | +The library support of P3068R6 (as amended by P3842R2) is now implemented in |
| 36 | +C++26 mode (or with --set_flag=constexpr_exceptions): std::rethrow_exception |
| 37 | +can be called during constant evaluation (even if the library declares or |
| 38 | +defines it without "constexpr"), and so can std::exception_ptr operations if |
| 39 | +that class has a single nonstatic data member of pointer type (as in the GCC |
| 40 | +C++ standard library). For example, with --c++26 and a library that supports |
| 41 | +this: |
| 42 | + |
| 43 | + #include <exception> |
| 44 | + constexpr int f() { |
| 45 | + std::exception_ptr p = std::make_exception_ptr(42); |
| 46 | + try { |
| 47 | + std::rethrow_exception(p); |
| 48 | + } catch (int i) { |
| 49 | + return i; |
| 50 | + } |
| 51 | + } |
| 52 | + static_assert(f() == 42); // Previously an error; now okay. |
| 53 | + |
| 54 | +std::current_exception and std::uncaught_exceptions are not constexpr |
| 55 | +functions (P3842R2), so calls to them are still not constant expressions. |
| 56 | +However, in g++ modes with gnu_version >= 160000, the builtins |
| 57 | +__builtin_eh_ptr_adjust_ref, __builtin_current_exception, and |
| 58 | +__builtin_uncaught_exceptions are now supported, including during constant |
| 59 | +evaluation. The GCC C++ standard library uses the first two to implement |
| 60 | +std::exception_ptr and std::make_exception_ptr. |
| 61 | + |
| 62 | +In addition, a dynamic_cast to a reference type that fails, typeid applied to |
| 63 | +a null glvalue, and an array new-expression with an invalid number of elements |
| 64 | +now throw std::bad_cast, std::bad_typeid, and std::bad_array_new_length, |
| 65 | +respectively, during constant evaluation, provided that the definition of the |
| 66 | +corresponding class is available and it has a constexpr default constructor. |
| 67 | +Otherwise, those cases are diagnosed as before. |
| 68 | + |
| 69 | +An exception object that is still referred to by a std::exception_ptr at the |
| 70 | +end of the evaluation causes the evaluation to not be constant. |
| 71 | + |
| 72 | + |
| 73 | +11/12/26 [GH #500] |
| 74 | +Empty enclosing packs in generic lambdas |
| 75 | + |
| 76 | +A pack expansion in a generic lambda nested in a template instantiation could |
| 77 | +produce an error node in the IL when it referenced a lambda parameter pack |
| 78 | +before an empty enclosing parameter pack. Empty enclosing packs are now |
| 79 | +handled correctly regardless of the order of the pack references. Spurious |
| 80 | +warnings from prototype instantiations in such cases are also suppressed. For |
| 81 | +example, with --c++20 -tused: |
| 82 | + |
| 83 | + template<int... Is> void f() { |
| 84 | + []<int... Js>() { ((Js * Is) * ... * 1); }; |
| 85 | + } |
| 86 | + template void f<>(); // Previously produced an error node in the IL. |
| 87 | + |
| 88 | + |
| 89 | +11/12/26 [GH #506] |
| 90 | +Pack expansions in nested generic-lambda template default arguments |
| 91 | + |
| 92 | +A default template argument of a nested generic lambda could be rejected when a |
| 93 | +pack expansion involving an enclosing pack was incorrectly treated as empty |
| 94 | +before the lambda's template arguments were available. Such defaults now |
| 95 | +remain dependent until instantiation. For example, with --c++20 -tused: |
| 96 | + |
| 97 | + template<int... Is> void f() { |
| 98 | + [](auto) { |
| 99 | + []<int... Js, typename U = int[((Js * Is) * ... * 1) - 2]>() {}; |
| 100 | + }; |
| 101 | + } |
| 102 | + template void f<2>(); // Previously a spurious array bound error, now okay. |
| 103 | + |
| 104 | + |
3 | 105 | 11/10/26 [GH #501] |
4 | 106 | Array new with a wide string literal initializer in constant evaluation |
5 | 107 |
|
|
0 commit comments