Bug 121521 - constexpr structured binding rejects tuple-protocol objects
Summary: constexpr structured binding rejects tuple-protocol objects
Status: RESOLVED DUPLICATE of bug 117784
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 16.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-08-12 16:27 UTC by Desmond Gold
Modified: 2025-08-12 16:36 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Desmond Gold 2025-08-12 16:27:52 UTC
#include <array>

struct Point {
    int x, y, z;
};

int main() {
    constexpr int arr[] {1, 2, 3};
    constexpr auto [a, b, c] = arr;

    constexpr std::array std_arr {4, 5, 6};
    constexpr auto [d, e, f] = std_arr; // error
    constexpr auto [g, h, i] = std::array{42, 91, 72}; // error

    constexpr auto [x, y, z] = Point {9, 0, -4};
}

From the snippet of code above, constexpr structured bindings have no problems with native arrays and aggregate types. But with std::array (has a member array and uses the tuple protocol), the compiler rejects two lines:

<source>:12:32: error: '<structured bindings>.std::array<int, 3>::_M_elems[0]' is not a constant expression
   12 |     constexpr auto [d, e, f] = std_arr; // error
      |                                ^~~~~~~
<source>:12:32: error: '<structured bindings>.std::array<int, 3>::_M_elems[1]' is not a constant expression
<source>:12:32: error: '<structured bindings>.std::array<int, 3>::_M_elems[2]' is not a constant expression
<source>:13:53: error: '<structured bindings>.std::array<int, 3>::_M_elems[0]' is not a constant expression
   13 |     constexpr auto [g, h, i] = std::array{42, 91, 72}; // error
      |                                                     ^
<source>:13:53: error: '<structured bindings>.std::array<int, 3>::_M_elems[1]' is not a constant expression
<source>:13:53: error: '<structured bindings>.std::array<int, 3>::_M_elems[2]' is not a constant expression
Comment 1 Desmond Gold 2025-08-12 16:31:23 UTC
Edited testcase:

#include <array>
#include <tuple>

struct Point {
    int x, y, z;
};

int main() {
    constexpr int arr[] {1, 2, 3};
    constexpr auto [a, b, c] = arr;

    static constexpr std::array std_arr {4, 5, 6};
    constexpr auto [d, e, f] = std_arr; // error
    constexpr auto [g, h, i] = std::array{42, 91, 72}; // error

    constexpr auto [j, k] = std::make_tuple(1, 3.14f); // error
    constexpr auto [l, m] = std::pair {'A', true}; // error

    constexpr auto [x, y, z] = Point {9, 0, -4};
}

In general, it rejects lines that accept objects of types with tuple protocol.
Comment 2 Jakub Jelinek 2025-08-12 16:36:07 UTC
https://gcc.gnu.org/projects/cxx-status.html#cxx26 clearly states that the whole paper has not been implemented yet, only the smaller part of it which allows constexpr and constinit keyword on structured bindings.
So, in some cases for now the structured binding needs to be static if it needs to be constexpr, because the references to constexpr vars part of the paper is not implemented yet.
PR117784 says that too.

*** This bug has been marked as a duplicate of bug 117784 ***