Bug 117784

Summary: [C++26] P2686R4 - constexpr structured bindings and references to constexpr variables
Product: gcc Reporter: Jakub Jelinek <jakub>
Component: c++Assignee: Marek Polacek <mpolacek>
Status: ASSIGNED ---    
Severity: normal CC: cooky.ykooc922, daniel.kruegler, mpolacek, pilarlatiesa, tkaminsk, webrown.cpp
Priority: P3    
Version: 15.0   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122269
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2024-11-26 00:00:00
Bug Depends on:    
Bug Blocks: 110338    
Attachments: gcc16-pr117784-wip.patch

Description Jakub Jelinek 2024-11-26 09:29:49 UTC
See <https://wg21.link/P2686R4>.
Comment 1 Jakub Jelinek 2025-06-18 15:12:41 UTC
Created attachment 61661 [details]
gcc16-pr117784-wip.patch

Untested patch for the easy part of the paper (namely allowing constexpr and constinit (the latter we've been allowing quietly) and propagating it to the sb vars.
The patch doesn't implement anything from the constant expression references/pointers changes, so with the patch constexpr & constinit sb work solely with namespace scope or static or the automatic non-tuple case without ref qualifiers.
Comment 2 GCC Commits 2025-07-08 17:22:38 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:c81447d969f27a8653ebb1a450372f0d25a2e628

commit r16-2108-gc81447d969f27a8653ebb1a450372f0d25a2e628
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Jul 8 19:21:55 2025 +0200

    c++: Implement part of C++26 P2686R4 - constexpr structured bindings [PR117784]
    
    The following patch implements the constexpr structured bindings part of
    the P2686R4 paper, so the [dcl.pre], [dcl.struct.bind], [dcl.constinit]
    and first hunk in [dcl.constexpr] changes.
    The paper doesn't have a feature test macro and the constexpr structured
    binding part of it seems more-less self-contained, so I think it is useful
    to get this in independently from the rest.
    Of course, automatic constexpr/constinit structured bindings in the
    tuple cases or automatic constexpr/constinit structured bindings with auto &
    will not really work for now.
    Another reason for the split is that for C++ < 26, I think what the patch
    implements is basically what the users will see, i.e. we can accept
    constexpr or constinit structured binding with pedwarn, but I think we can't
    change the constant expression rules in C++ < 26.
    
    I plan to look at the rest of the paper.
    
    2025-07-08  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/117784
            * decl.cc: Implement part of C++26 P2686R4 - constexpr structured
            bindings.
            (cp_finish_decl): Pedwarn for C++23 and older on constinit on
            structured bindings except for static/thread_local where it uses
            earlier error.
            (grokdeclarator): Pedwarn on constexpr structured bindings for
            C++23 and older instead of emitting error always, don't clear
            constexpr_p in that case.
            * parser.cc (cp_parser_decomposition_declaration): Copy over
            DECL_DECLARED_CONSTEXPR_P and DECL_DECLARED_CONSTINIT_P flags.
    
            * g++.dg/cpp1z/decomp3.C (test): For constexpr structured binding
            initialize from constexpr var instead of non-constexpr and expect
            just a pedwarn for C++23 and older instead of error always.
            * g++.dg/cpp26/decomp9.C (foo): Likewise.
            * g++.dg/cpp26/decomp22.C: New test.
            * g++.dg/cpp26/decomp23.C: New test.
            * g++.dg/cpp26/decomp24.C: New test.
            * g++.dg/cpp26/decomp25.C: New test.
Comment 3 Jakub Jelinek 2025-07-08 17:25:21 UTC
As the commit message says, so far partially implemented.  One can declare structured bindings constexpr as long as the C++23-ish constant expression handling allows that.
Comment 4 GCC Commits 2025-07-15 15:34:40 UTC
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:9956dc37cb3d71c1fc7a89b45cc291645c11817b

commit r16-2264-g9956dc37cb3d71c1fc7a89b45cc291645c11817b
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Jul 15 10:46:41 2025 -0400

    c++: decomp25 vs -fimplicit-constexpr [PR117784]
    
    Avoid diagnostic changes from -fimplicit-constexpr.
    
            PR c++/117784
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp26/decomp25.C: Add -fno-implicit-constexpr.
Comment 5 Jakub Jelinek 2025-08-12 16:36:07 UTC
*** Bug 121521 has been marked as a duplicate of this bug. ***
Comment 6 Tomasz KamiƄski 2025-10-14 09:41:25 UTC
*** Bug 122269 has been marked as a duplicate of this bug. ***