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
143
votes
4 answers

Lambda implicit capture fails with variable declared from structured binding

With the following code, I get a compile error C2065 'a': undeclared identifier (using visual studio 2017): [] { auto [a, b] = [] {return std::make_tuple(1, 2); }(); auto r = [&] {return a; }(); //error C2065 }(); However, the following…
123
votes
3 answers

std::ignore with structured bindings?

Prelude: std::tuple f(); std::tuple g(); C++1z will introduce syntax for structured bindings which will make it possible to write instead of int a, b, c; std::tie(a, b, c) = f(); something like auto [a, b, c] =…
jotik
  • 17,044
  • 13
  • 58
  • 123
70
votes
2 answers

Are nested structured bindings possible?

Assume I have an object of type std::map> data; Is it possible to access the element types in a nested way (i.e. when used in ranged for loop) like this for (auto [str, [my_int, my_float]] : data) /* do something…
Timo
  • 9,269
  • 2
  • 28
  • 58
57
votes
5 answers

Why do C++17 structured bindings not use { }?

I found the original proposal for *C++ structured bindings here. It proposes a way to easily bind multiple return values, i.e.: auto {a, b} = minmax(data); But now I see that everyone points to the C++17/C++1z proposal syntax of auto [a, b] =…
towi
  • 21,587
  • 28
  • 106
  • 187
51
votes
2 answers

structured binding with [[maybe_unused]]

Functional languages with pattern matching (sometimes?) have the possibility to ignore some bound values, but with C++17 structured bindings there seem to be no way to do that (std::ignore with structured bindings?). The advice is to use a dummy…
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
50
votes
3 answers

Does structured binding work with std::vector?

Is it possible to use structured binding with vectors? E.g. std::vector vec{1, 2, 3}; auto [a, b, c] = vec; Above code unfortunately doesn't work (under GCC), but maybe there's a different way (with structured binding) that allows to assign…
BartekPL
  • 2,290
  • 1
  • 17
  • 34
46
votes
2 answers

Can the structured bindings syntax be used in polymorphic lambdas

Structured bindings make it more clean and readable to iterate through a map with a range based for loop like below for (auto [key, value] : map) { cout << key << " : " << value << endl; } But can structured bindings be used in lambda…
Curious
  • 20,870
  • 8
  • 61
  • 146
41
votes
1 answer

Why can't decomposition declarations be constexpr?

Consider the following snippet to test the upcoming C++17 feature decomposition declarations (formerly known as structured bindings) #include #include constexpr auto divmod(int n, int d) { return std::make_pair(n / d, n %…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
39
votes
3 answers

C++17 structured binding that also includes an existing variable

This SO answer lists some shortcomings of C++17 decomposition declarations (the feature formerly known as "structured binding"). For example, you can't give explicit types to the new variables, and so on. But one big shortcoming I'm running into…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
31
votes
1 answer

Do const references in structured bindings extend the lifetime of the decomposed object?

Does writing const auto& [a, b] = f(); guarantee extending the lifetime of the object returned from f(), or at least the objects a and b are bound to? Reading through the proposal I don't see anything obvious in the language to make me sure that it…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
30
votes
4 answers

What are use cases for structured bindings?

C++17 standard introduces a new structured bindings feature, which was initially proposed in 2015 and whose syntactic appearance was widely discussed later. Some uses for them come to mind as soon as you look through documentation. Aggregates…
Sergey
  • 7,985
  • 4
  • 48
  • 80
27
votes
1 answer

Is decltype(auto) for a structured binding supposed to be a reference?

Consider an example: #include #include #include int main() { auto tup = std::make_tuple(1, 2); auto [ a, b ] = tup; decltype(auto) e = a; std::cout << std::boolalpha <<…
W.F.
  • 13,888
  • 2
  • 34
  • 81
26
votes
1 answer

Shall structured binding to a copy of a const c-array be const?

Consider this code (demo): #include #include struct Ag{int i;int j;}; using T = std::tuple; using Ar = int[2]; const Ag ag {}; const T t {}; const Ar ar {}; void bind_ag(){ auto [i,j] = ag; …
Oliv
  • 17,610
  • 1
  • 29
  • 72
26
votes
1 answer

Why Structured Bindings disable both RVO and move on return statement?

Suppose we have a class named AAA that supports both copy/move: class AAA { public: AAA() = default; ~AAA() = default; AAA(const AAA& rhs) { std::cout << "Copy constructor" << std::endl; } AAA(AAA&& rhs) { …
Dean Seo
  • 5,486
  • 3
  • 30
  • 49
25
votes
2 answers

Does copy elision work with structured bindings

Does mandatory copy elision apply to decomposition via structured bindings? Which of the following cases does that apply to? // one auto [one, two] = std::array{SomeClass{1}, SomeClass{2}}; // two auto [one, two] =…
Curious
  • 20,870
  • 8
  • 61
  • 146
1
2 3
9 10