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
0
votes
1 answer

structured bindings to array element

What is the correct syntax of structured bindings when the left-hand side is a reference to an array member? For example, the following program does not compile: #include std::array f() { return { 1, 2 }; } int main() { …
francesco
  • 7,189
  • 7
  • 22
  • 49
0
votes
1 answer

Structured bindings on a map of string to string not working

This is my code: std::map map = {{"a","b"},{"c","d"}}; for(auto& [key,value] : map) { key = std::string("c"); value = std::string("c"); } and when i compile it i get error: no viable overloaded '=' what i'm doing…
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
0
votes
0 answers

Boost zip-iterator and structured bindings

I was messing around with a wrapper class for "structure of arrays" and ended up finding "boost-iterators's" zip-iterator. Now I was wondering if there's a way to make the resulting tuple from iterating, work with structured bindings like in the…
Yamahari
  • 1,926
  • 9
  • 25
0
votes
1 answer

Can't get std::ignore working in structured bindings in for range

This code compiles and works, with an 'unused variable i' warning: for(auto [camera, i]: landmark->getObservations()) camerasToCounts[camera]++; I want to ignore i, so I replaced it with std::ignore. The following code doesn't…
Alejandro Silvestri
  • 3,706
  • 31
  • 43
0
votes
0 answers

GCC Segmentation fault for this structured binding using Qt

I got a segmentation fault for this code in GCC but compiles fine with MSVC QMap> someData; // inserting some value for (auto id : someData.keys()) { auto [foo, bar] = someData[id]; //do stuff with foo,…
Bako
  • 313
  • 1
  • 15
0
votes
0 answers

Range-based for loop, auto, tuples and structured bindings

for (auto [i, j] : vector>{{1, 7}, {3, 2}}) cout << i << j; Is there a way to make this range-based for loop more concise by omitting type specification of the container? I don't care about its actual type, as long as it contains…
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
0
votes
1 answer

Why can't I access the vector?

I am currently trying to access the vector defined as such: #include #include #include #include using namespace std; template class file { public: typedef vector >…
Lamda
  • 914
  • 3
  • 13
  • 39
-1
votes
1 answer

Using structure binding to change the value of a custom structure

I am trying to find out a way to change the value of a custom structure using structured binding .I was able to do with std::map.I referred few materials from Structured binding In the below code I was able to change the value of a map . I want to…
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
-2
votes
1 answer

Structured bindings with the same names

I am writing some OpenGL app, whatever. In one place I've decided to use structured bindings, cuz it would save me a lot of typing. Here it is: for (auto row = 0; row < N - 1; ++row) { for (auto col = 0; col < N - 1; ++col) { …
minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57
1 2 3
9
10