-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] Constexpr Structured Bindings : The easy parts #160337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cor3ntin
merged 3 commits into
llvm:main
from
cor3ntin:corentin/structured_bindings_pt1
Sep 24, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// RUN: %clang_cc1 -std=c++2c %s -triple x86_64-unknown-linux-gnu -verify=expected | ||
// RUN: %clang_cc1 -std=c++2c %s -triple x86_64-unknown-linux-gnu -verify=expected -fexperimental-new-constant-interpreter | ||
|
||
namespace std { | ||
using size_t = decltype(sizeof(0)); | ||
template<typename> struct tuple_size; | ||
template<size_t, typename> struct tuple_element; | ||
} | ||
|
||
struct Y { int n = 0; }; | ||
struct X { X(); X(Y); X(const X&); ~X(); int k = 42;}; // #X-decl | ||
struct Z { constexpr Z(): i (43){}; int i;}; // #Z-decl | ||
struct Z2 { constexpr Z2(): i (0){}; int i; ~Z2();}; // #Z2-decl | ||
|
||
struct Bit { constexpr Bit(): i(1), j(1){}; int i: 2; int j:2;}; | ||
|
||
struct A { int a : 13; bool b; }; | ||
|
||
struct B {}; | ||
template<> struct std::tuple_size<B> { enum { value = 2 }; }; | ||
template<> struct std::tuple_size<const B> { enum { value = 2 }; }; | ||
template<> struct std::tuple_element<0, const B> { using type = Y; }; | ||
template<> struct std::tuple_element<1, const B> { using type = const int&; }; | ||
template<int N> | ||
constexpr auto get(B) { | ||
if constexpr (N == 0) | ||
return Y(); | ||
else | ||
return 0.0; | ||
} | ||
|
||
|
||
constexpr auto [t1] = Y {42}; | ||
static_assert(t1 == 42); | ||
|
||
constexpr int i[] = {1, 2}; | ||
constexpr auto [t2, t3] = i; | ||
static_assert(t2 == 1); | ||
static_assert(t3 == 2); | ||
|
||
constexpr auto [t4] = X(); | ||
// expected-error@-1 {{constexpr variable cannot have non-literal type 'const X'}} \ | ||
// expected-note@#X-decl {{'X' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors}} | ||
|
||
constexpr auto [t5] = Z(); | ||
static_assert(t5 == 43); | ||
|
||
constexpr auto [t6] = Z2(); | ||
//expected-error@-1 {{constexpr variable cannot have non-literal type 'const Z2'}} | ||
// expected-note@#Z2-decl {{'Z2' is not literal because its destructor is not constexpr}} | ||
|
||
constexpr auto [t7, t8] = Bit(); | ||
static_assert(t7 == 1); | ||
static_assert(t8 == 1); | ||
|
||
void test_tpl(auto) { | ||
constexpr auto [...p] = Bit(); | ||
static_assert(((p == 1) && ...)); | ||
} | ||
|
||
void test() { | ||
test_tpl(0); | ||
} | ||
|
||
// FIXME : support tuple | ||
constexpr auto [a, b] = B{}; | ||
static_assert(a.n == 0); | ||
// expected-error@-1 {{static assertion expression is not an integral constant expression}} \ | ||
// expected-note@-1 {{read of temporary is not allowed in a constant expression outside the expression that created the temporary}}\ | ||
// expected-note@-2 {{temporary created here}} | ||
|
||
constinit auto [init1] = Y {42}; | ||
constinit auto [init2] = X {}; // expected-error {{variable does not have a constant initializer}} \ | ||
// expected-note {{required by 'constinit' specifier here}} \ | ||
// expected-note {{non-constexpr constructor 'X' cannot be used in a constant expression}} \ | ||
// expected-note@#X-decl {{declared here}} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: timem