From ae4fe2bbc6dc2857785f7337d0351e4ee535be7f Mon Sep 17 00:00:00 2001 From: Eisenwave Date: Sun, 8 Mar 2026 21:14:55 +0100 Subject: [PATCH] Add summary and explanation of P1789 --- .../expansion-statements-library-support.md | 40 +++++++++++++++++++ features_cpp26.yaml | 2 + 2 files changed, 42 insertions(+) create mode 100644 content/expansion-statements-library-support.md diff --git a/content/expansion-statements-library-support.md b/content/expansion-statements-library-support.md new file mode 100644 index 0000000..b9233b1 --- /dev/null +++ b/content/expansion-statements-library-support.md @@ -0,0 +1,40 @@ +--- +execute: true +--- + +## What It Does + +`std::integer_sequence` can now be used in `template for` +to conveniently write a loop where the loop index is a `constexpr` variable, +as well as to create a `constexpr` pack using structured bindings. +This is made possible by specializing `std::tuple_size`, `std::tuple_element`, and `std::get` +for `std::integer_sequence`. + +## Why It Matters + +These changes remove boilerplate code such as creating an immediately invoked lambda expression +when creating a `constexpr` pack of indices or writing a loop with a `constexpr` index. +This is especially important because C++26 supports pack indexing, +where a `constexpr` index is needed. + +## Example + +```cpp +#include +#include +#include + +int main() { + auto tup = std::tuple{1, 3.14, "hello"}; + + // Output: + // tup[0] = 1 + // tup[1] = 3.14 + // tup[2] = hello + template for (constexpr std::size_t I : std::make_index_sequence<3>()) { + std::println("tup[{}] = {}", I, std::get(tup)); + } + + constexpr auto [...Is] = std::make_index_sequence<3>(); +} +``` diff --git a/features_cpp26.yaml b/features_cpp26.yaml index a358780..fc21536 100644 --- a/features_cpp26.yaml +++ b/features_cpp26.yaml @@ -1602,6 +1602,8 @@ features: - desc: "Library Support for Expansion Statements" paper: P1789 + summary: "Specializes `std::tuple_size`, `std::tuple_element`, and `std::get` for `std::integer_sequence` to make it usable in expansion statements and structured bindings." + content: expansion-statements-library-support.md lib: true support: [Clang 22] ftm: