Questions tagged [structured-bindings]

Structured bindings are a feature in C++17 (previously called C++1z) that allow for declaring multiple variables initialized from a tuple or struct.

Structured bindings are a feature in C++17 (previously called C++1z) that allow for declaring multiple variables initialized from a tuple or struct.

Given a function defined:

tuple<T1,T2,T3> 
f(/*...*/) {
    /*...*/ 
    return {a,b,c};
}

It can be used to initialize three local variables like so:

auto [x, y, z] = f()

The original proposal is available as P0144 on the C++ Standards Committee website.

144 questions
24
votes
2 answers

Why structured bindings only work with auto

Structured bindings have been introduced with c++17. They give the ability to declare multiple variables initialised from a tuple or struct. This code compiles using a c++17 compiler. #include #include int main() { auto tuple…
schorsch312
  • 5,553
  • 5
  • 28
  • 57
23
votes
2 answers

structured bindings with std::minmax and rvalues

I ran into a rather subtle bug when using std::minmax with structured bindings. It appears that passed rvalues will not always be copied as one might expect. Originally I was using a T operator[]() const on a custom container, but it seems to be the…
Zulan
  • 21,896
  • 6
  • 49
  • 109
23
votes
1 answer

Variables marked as const using structured bindings are not const

I have been writing a set of classes to allow for a simple python-like zip-function. The following snippet works (almost) just as expected. However, the two variables a and b are not const. std::vector v1{0.0, 1.1, 2.2,…
Jonas
  • 6,915
  • 8
  • 35
  • 53
23
votes
2 answers

structured bindings: when something looks like a reference and behaves similarly to a reference, but it's not a reference

Yesterday I've seen an interesting question here on SO about structured binding. We can sum up it as it follows. Consider the example code below: #include #include int main() { auto tup = std::make_tuple(1, 2); auto &…
skypjack
  • 49,335
  • 19
  • 95
  • 187
22
votes
4 answers

Structured binding to replace std::tie abuse

In reading this summary of the c++17 final features I was a bit surprised by the section on structured bindings (emphasis mine): structured bindings Until now, there was a known trick to abuse std::tie to assign a tuple or pair to different…
Emerald Weapon
  • 2,392
  • 18
  • 29
21
votes
1 answer

Structured binding in lambda arguments

Why I cannot use C++17 structured binding in this case? std::map m; std::find_if( m.cbegin(), m.cend(), []( const auto & [x, y] ){ return x == y; } );
Kill KRT
  • 1,101
  • 2
  • 17
  • 37
21
votes
1 answer

Why are structured bindings in range-based for-loop just a copy and not a reference?

I have the following code: #include "stdafx.h" #include #include int main() { struct Foo { int a; }; std::unordered_map foos{ { 0, { 3 } }, { 1, { 4 } } }; for (auto &[i, foo] : foos) { …
Jupiter
  • 1,421
  • 2
  • 12
  • 31
21
votes
2 answers

Why does including break structured bindings in GCC?

Consider: struct Point { int x, y; }; int main() { const auto [x, y] = Point{}; } This code compiles fine with gcc 7.1 in C++17 mode, however this one: #include struct Point { int x, y; }; int main() { const auto [x, y] =…
robson3.14
  • 3,028
  • 2
  • 20
  • 19
18
votes
2 answers

Why does "const auto [x, y]" not behave as expected when binding to reference types?

The following code snippet is excerpted from cppref: std::tuple f(); auto [x, y] = f(); // decltype(x) is int // decltype(y) is int& const auto [z, w] = f(); // decltype(z) is const int // decltype(w) is int& My question is at the…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
17
votes
1 answer

Why does structured binding not work as expected on struct?

struct X { int a, b; }; int main() { auto p = std::pair{ 1, 2 }; const auto&[r1, r2] = p; // ok X x{ 1, 2 }; const auto&[r3, r4] = x; // error } clang 7.0 (on Windows) 's error message: error : cannot decompose this type;…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
17
votes
1 answer

structured binding initializer inside if statement does not compile

Reading up on C++17 and now multiple initializations inside if statement is possible: if (int x = func(), y = func2(); x > 0 && y > 0) { } Nice one, also in combination with another feature in C++17, structured bindings: if (auto[iter, success] =…
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
17
votes
3 answers

Why ADL does not resolve to the correct function with std::get

I am trying to code a template function that uses an ADL resolved get to fetch members of a struct/range (tuple-esque). #include #include #include int main() { auto tup = std::make_tuple(1, 2); std::cout <<…
Curious
  • 20,870
  • 8
  • 61
  • 146
15
votes
1 answer

Do structured bindings and forwarding references mix well?

I know I can do auto&& bla = something(); and depending on the constness of the return value of something, I'd get a different type for bla. Does this also work in the structured bindings case, e.g. auto&& [bla, blabla] = something(); I would…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
15
votes
2 answers

Structured bindings for your own type that isn’t a struct or a tuple(via public member function)

I am going through the Herb Sutter's A journey: Toward more powerful and simpler C++ programming Structure Binding section In order to understand the concept .Best is to write a program I tried but getting some error Just want to try how to use…
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
14
votes
1 answer

Range-based for loop on unordered_map and references

When running a range-based for loop on an std::unordered_map it appears that the type of the loop variable does not use reference types: std::unordered_map map = { {0, 1}, {1, 2}, {2, 3} }; for(auto&[l, r] : map) …
1
2
3
9 10