When I compile this code:
std::tuple<int, int> array[] = {std::make_tuple(1, 2), std::make_tuple(1, 2),
std::make_tuple(1, 2), std::make_tuple(1, 2)};
for (auto[a, b] : array) {
printf("%u %u", a, b);
}
if (auto[a, b] = std::forward_as_tuple(1, 2); b != 0xff) {
printf("%u %u", a, b);
}
while (auto[a, b] = std::make_tuple(1, 2); b != 0xff) {
printf("%u %u", a, b);
}
With:
clang++ -std=c++1z
I get the following errors:
main2.cpp:76:14: error: decomposition declaration not permitted in this context
while (auto[a, b] = std::make_tuple(1, 2); b != 0xff) {
^~~~~~
main2.cpp:76:46: error: use of undeclared identifier 'b'
while (auto[a, b] = std::make_tuple(1, 2); b != 0xff) {
^
2 errors generated.
Why is auto[a, b] = std::forward_as_tuple(1, 2); b != 0xff
auto[a, b] = std::forward_as_tuple(1, 2); b != 0xff
supported in an if
but not in a while
? Is there some technical reason or is it a "that's just the way it is" reason?
According to the latest draft standard for C++, the while
loop does not in fact have an optional init-statement
the kind that if
and switch
gained in C++17.
The formal syntax is:
while ( condition ) statement
In conclusion, the structured binding is not the issue here. Check this segment of the draft for reference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.