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

Why does std::initializer_list not support std::get<>, std::tuple_size and std::tuple_element

Why does std::initializer_list not support std::get<>, std::tuple_size and std::tuple_element? It is used a lot in constexpr expressions as is right now, for example, std::max({1, 2, 3, 4, 5}); And if it did cool things like the following would…
Curious
  • 20,870
  • 8
  • 61
  • 146
3
votes
1 answer

Structured bindings and mandatory copy elision

If you utilize structured bindings like so auto [a, b, c] = std::make_tuple(1, 10.0, "string object"s); then will the copies from the returned tuple be elided and the objects go straight into a, b and c or will the initializations be move…
Curious
  • 20,870
  • 8
  • 61
  • 146
2
votes
2 answers

Decomposition of tuple with structured bindings into const, and const& variables

Trying to understand working of structured bindings with const and references in particular as std::tuple is decomposed into named variables. In the following case, it makes sense that a would be of type const int, with int& for b since int& const…
xyf
  • 664
  • 1
  • 6
  • 16
2
votes
1 answer

Using `static` keyword with Structured Binding

I'm trying to use C++17 structured binding to return a pair of values and I want those values to be both static and const so that they are computed the first time the function they're in is called and then they maintain their uneditable values for …
bcaddy
  • 73
  • 5
2
votes
0 answers

Can I partly bind a tuple using a structured binding with fewer elements than the size of the tuple?

I'm surprised I couldn't find this question asked anywhere. Also it would seem this language feature would not be hard to implement (may be wrong though). const auto [a, b] = function_returning_triplet(); // error: only 2 names provided for…
haelix
  • 4,245
  • 4
  • 34
  • 56
2
votes
0 answers

`ref-qualified` `identifier-list` is not a reference in C++ Structured binding but act as one

In C++ Structured binding if we use & as the ref-qualifier like bellow: tuple tpl{ 1 }; auto& [myInt] = tpl; when we assign to myInt, changes are reflected to tpl: myInt = -1; cout << get<0>(tpl) << " " << endl; // -1 Here myInt ac as a…
Mohammad Rahimi
  • 965
  • 5
  • 15
2
votes
2 answers

Structured binding reference with tuple of reference

The Structured binding Case2 in cppreference is a bit hard to understand. Basically, I want a clarification of these situations int x = 1; double y = 2.0; auto [a, b] = std::forward_as_tuple(x, y); //a, b are both reference, why? auto&& [c, d] =…
sz ppeter
  • 1,698
  • 1
  • 9
  • 21
2
votes
1 answer

c++17 structured bindings and move semantics

im reading this https://leanpub.com/cppmove (C++17 - The Complete Guide First Edition Nicolai M. Josuttisand) and regarding c++17 structured bindings and move semantics it gives the following example, where the moved from object ms is left…
mrchance
  • 1,133
  • 8
  • 24
2
votes
1 answer

What part in cppreference tells me structured binding declarations only work with compile-time known objects?

My question is What part of the cppreference.com's page on structured binding declarations should make apparent that they cannot be used with "things" not known at compile time? That page does not contain any explicit reference to compile or run…
Enlico
  • 23,259
  • 6
  • 48
  • 102
2
votes
1 answer

Why does structured binding introduce variables as values, not references?

I'm learning about structured binding declarations. My understanding was that in auto& [x, y] = expr; variables x and y are introduced of types "reference to std::tuple_element::type" (for i=0, 1 and E is the type of the invisible variable e).…
yeputons
  • 8,478
  • 34
  • 67
2
votes
0 answers

Discrepancy compiler output for structured-bindings with ref-qualifier in C++17

Consider the snippet below: #include double refd = 5.0; int refi = 1; decltype(auto) foo(){return std::tuple{refd,refi};} auto&[d,i] = foo(); //msvc:pass, gcc:fail, clang:fail Available on compiler explorer In the case of…
Naebaf
  • 321
  • 1
  • 8
2
votes
2 answers

Returning tuple of local objects

How does one exploit structured binding and tuples to return objects local to a function? In a function, I am making local objects that reference each other, and I want to return these objects in a tuple and use structured binding to identify them…
Anthony
  • 1,015
  • 8
  • 22
2
votes
0 answers

C++17 Structured binding for class member with this operator

Why C++17 structured binding do not like the this-> operator in is a syntax like this one : std::tuple external_function() { return { 0, "" }; } class Foo { public: void internal_method() { auto [this->x,…
MaxC2
  • 343
  • 3
  • 10
2
votes
1 answer

qtcreator does not autocomplete when structure bindings is used?

I seem to have an issue with qtcreator not autocompleting my code, which is getting pretty annoying. Currently is it not able to autocomplete when i try to use structure bindings in for loops like this.. std::vector>>…
Lamda
  • 914
  • 3
  • 13
  • 39
2
votes
1 answer

function declaration for structured bindings

Can structured bindings only be used with some kind of "struct" as return value? Give back any of class/struct e.g. a tuple here works fine: auto f() { return std::make_tuple(1,2.2); } Is there something which enables something like: auto…
Klaus
  • 24,205
  • 7
  • 58
  • 113