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

Are structured bindings reusable?

I'm using Windows 10, Visual Studio 2017 v15.7.1 with /std:c++latest /permissive- This simple code with structured bindings won't compile: auto [a, b] = func1(x, y, z); // auto func1() -> std::tuple [a, b] = func2(x, y, z);…
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
14
votes
1 answer

Why do structured bindings depend on tuple_element?

The most recent draft of the structured bindings proposal (on which the C++17 feature was based) requires std::tuple_size, member get or std::get, and std::tuple_element. Previous drafts require only std::tuple_size and member get or std::get. As…
David Stone
  • 26,872
  • 14
  • 68
  • 84
14
votes
2 answers

structured bindings and range-based-for; supress unused warning in gcc

I want to traverse a map using structure bindings, ignoring the key: for (auto& [unused, val] : my_map) do_something(val); I have tried different options with gcc-7.2.0: // The warning is issued for ([[maybe_unused]] auto& [unused, val] :…
ABu
  • 10,423
  • 6
  • 52
  • 103
13
votes
2 answers

Structured binding on const

Is the following code supposed to compile? #include void foo() { const std::pair x = {1, 2}; auto [a, b] = x; static_assert(std::is_const_v); static_assert(std::is_const_v); } MSVC says…
BiagioF
  • 9,368
  • 2
  • 26
  • 50
13
votes
2 answers

If structured bindings cannot be constexpr why can they be used in constexpr function?

According to this answer apparently there is no good reason why structured bindings are not allowed to be constexpr, yet the standard still forbids it. In this case, however, shouldn't the use of the structured bindings inside the constexpr function…
W.F.
  • 13,888
  • 2
  • 34
  • 81
12
votes
1 answer

Understand structured binding in C++17 by analogy

I'm trying to understand structured binding introduced in C++17. The explanation on cppreference is not obvious to me, but it looks like cv-auto ref-operator [x, y, z] = ... is roughly equivalent to (not to consider array case) cv-auto ref-operator…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
12
votes
1 answer

Why does auto seemingly deduce a reference when using structured bindings?

Consider the following code that uses structured bindings from C++17: int a = 0, b = 0; auto [x, y] = std::tie(a, b); y = 1; std::cout << a << ' ' << b << '\n'; Since I used auto, I would expect the code to print 0 0 as y should be a copy. However,…
s3rvac
  • 9,301
  • 9
  • 46
  • 74
11
votes
2 answers

What are the types of identifiers introduced by structured bindings in C++17?

To my knowledge, identifiers introduced by structured bindings in C++17 are in fact references to some "hidden" variable. Such that auto [ a, b ] = std::make_tuple(1, 2); is kind-of equivalent to auto e = std::make_tuple(1, 2); auto& a =…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
10
votes
2 answers

Structured binding and tie()

Given these declarations: int a[3] {10,20,30}; std::tuple b {11,22,33}; I can use structured binding declarations to decode a and b: auto [x1,y1,z1] = a; auto [x2,y2,z2] = b; But if x1, y1, etc. already exist, what do I…
oz1cz
  • 5,504
  • 6
  • 38
  • 58
10
votes
1 answer

Auto range based structured bindings with vector

I'm trying to loop over a vector of tuples: std::vector> tupleList; By using a range based for loop with structured bindings: for (auto&& [x, y, z] : tupleList) {} But Visual Studio 2017 15.3.5 gives the error: cannot…
Eejin
  • 770
  • 1
  • 8
  • 29
9
votes
2 answers

Why do I need to move `std::unique_ptr`

Given the following code: #include #include struct A {}; struct B : public A {}; std::pair> GetBoolAndB() { return { true, std::make_unique() }; } std::unique_ptr GetA1() { auto[a, b] =…
9
votes
1 answer

Structured binding violations

The code as follows #include int main() { auto [a] = std::make_tuple(1); return [a]() -> int { return a; }(); } produces an error in clang 12: :6:13: error: 'a' in capture list does not name a variable return [a]() ->…
Fedor
  • 17,146
  • 13
  • 40
  • 131
9
votes
1 answer

Providing tuple-like structured binding access for a class

I'm trying to support tuple-like structured binding access for a class. For simplicity, I'll use the following class in the rest of this post: struct Test { int v = 42; }; (I'm aware that this class supports structured bindings out of the box…
mtvec
  • 17,846
  • 5
  • 52
  • 83
9
votes
1 answer

structured binding with existing vars not possible?

Is it possible to use already existing vars as target for return values in connection with structured bindings? auto f() { return std::make_tuple(1,2.2); } int main() { int x; double z; [ x, z ] = f(); // Did not work in gcc 7.1 …
Klaus
  • 24,205
  • 7
  • 58
  • 113
9
votes
2 answers

Perfect forwarding of variables declared with structured binding

I have a struct template struct Demo { T x; T y; }; and I'm trying to write a generic function similar to std::get for tuples that takes a compile-time index I and returns an lvalue reference to the I-th member of the struct if…
Corristo
  • 4,911
  • 1
  • 20
  • 36
1 2
3
9 10