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
5
votes
2 answers

Structured binding on an unknown custom structure

Short version: I would like to be able to convert a struct to a tuple. At least the type. In the code below, the convertToTuple function does not work, because variadic parameters cannot be used in structured bindings (as far as I know). The key…
zazi
  • 71
  • 4
5
votes
2 answers

Why are structured bindings defined in terms of a uniquely named variable?

Why are structured bindings defined through a uniquely named variable and all the vague "name is bound to" language? I personally thought structured bindings worked as follows. Given a struct: struct Bla { int i; short& s; double* d; }…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
5
votes
1 answer

Can you violate ODR with structured bindings on a class type

The structured bindings feature says that it goes with the tuple like decomposition if the tuple_size template is a complete type. What happens when std::tuple_size is a complete type for the given type at one point in the program and is not…
Curious
  • 20,870
  • 8
  • 61
  • 146
5
votes
1 answer

Why does clang++ report a structured binding with "value stored to '...' during its initialization is never read"?

I have the following test case: testcase("[room] exits") { auto [center, east, north, south, west] = make_test_rooms(); check_eq(center->east(), east); check_eq(center->north(), north); check_eq(center->south(), south); …
aghast
  • 14,785
  • 3
  • 24
  • 56
5
votes
0 answers

Unpack to function parameters

Is it theoretically possible to add a language feature to unpack structures into the function actual parameter lists? I mean the following. template< typename ...Ts > void f(Ts... values) { (std::cout << ... << values) << std::endl; } struct S {…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
5
votes
1 answer

Structured bindings implementation underground and std::tuple

Is it true, that structured bindings in clang (I use recently builded clang version 4.0.0 (trunk 282683)) are implemented using some stuff from , like braces-init lists may use stuff from ? I wrote simple code just to play…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
4
votes
1 answer

Why do implicitly-generated deduction guides result in different behavior of the structured binding declaration?

This is the minimal reproduction code: #include #include #include using std::pair; template inline constexpr bool check_v = std::is_rvalue_reference_v; template void f(T &&p){ auto [x,y]…
Dappur
  • 59
  • 5
4
votes
0 answers

Is clang always right in warning me to preventing copying?

Consider the following code: std::vector> v; for (const auto &p : v) { // ... } for (const auto &[first, second] : v) { // ... } There is a vector of pairs of chars and two ways to iterate over the elements using range…
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
4
votes
3 answers

Lack of type information in structured bindings

I just learned about structured bindings in C++, but one thing I don't like about auto [x, y] = some_func(); is that auto is hiding the types of x and y. I have to look up some_func's declaration to know the types of x and y. Alternatively, I could…
Cubi73
  • 1,891
  • 3
  • 31
  • 52
4
votes
1 answer

How can I use a structured binding to copy a tuple-like object with elements whose type is T&?

The origin of this problem is that I'm designing a 2-dimensional container implemented by std::vector. The result type of operator[] is a proxy class that has a fixed number of elements, and then I want to use structured binding with this proxy…
RedFog
  • 1,005
  • 4
  • 10
4
votes
1 answer

why structured bindings don't return references to struct members using `auto&` but the members themselves are returned

I thought that using structured bindings and auto& specifier I can obtain references to structure members and use them directly instead of going through the structure. However, the following code works and the static asserts hold: struct Test { …
dev65
  • 1,440
  • 10
  • 25
4
votes
1 answer

Structured bindings to get consecutive elements of a sub-vector without copying

I have a std::vector vec where consecutive blocks of 3 elements are of interest. For ease of processing, I'd like to extract such elements. Currently, the code looks like: const T& a = vec[i]; const T& b = vec[i + 1]; const T& c = vec[i +…
Paul92
  • 8,827
  • 1
  • 23
  • 37
4
votes
1 answer

Why structured binding fails with 'std::tie' d objects?

I was curious to do this asked problem in the following way: #include #include #include #include #include #include int main() { const std::set s{ 0, 1, 2, 3, 4, 5, 6, 7, 8 }; …
JeJo
  • 30,635
  • 6
  • 49
  • 88
4
votes
1 answer

structured bindings, reference ? Is it possible to remove them

Let's say I have such a tuple. std::tuple tuple{}; I want to do something like that : auto [i1, i2] = tuple; // Here i1 is lvalue reference, i2 is int i1 is a lvalue reference because the first value on the tuple is one lvalue…
Antoine Morrier
  • 3,930
  • 16
  • 37
4
votes
2 answers

Initializer list and structured bindings deduction ambiguity in C++17

I had always avoided initializations like the following const auto& x = a, y = b; const int* const x = ptr_1, *const y = ptr_2; // wot For the reason that the reference and pointer qualifiers don't apply to both the initializations. Granted it's…
Curious
  • 20,870
  • 8
  • 61
  • 146