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

cv-qualifier propagation in structured binding

As quoted in dcl.struct.bind, Let cv denote the cv-qualifiers in the decl-specifier-seq. Designating the non-static data members of E as m 0 , m 1 , m 2 , ... (in declaration order), each v i is the name of an lvalue that refers to the member m i…
Nimrod
  • 2,908
  • 9
  • 20
8
votes
1 answer

Introduced intermediate variable in structured binding definition?

In [dcl.struct.bind] 9.6.4, there is definition of structured binding when initializer is a class type with std​::​tuple_­size​::​value properly defined: ... variables are introduced with unique names ri as follows: S Ui ri = initializer ; Each…
Masquue
  • 166
  • 8
8
votes
1 answer

Unpacking variadic tuples in c++17

Is there anything better in c++17 (maybe C++2a) than the classic C++14 way to unpack variadic tuple with std::index_sequence? Anything better than this: template class MultiIterator { public: MultiIterator(I const& ...i) …
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
8
votes
1 answer

What type of structs can structured bindings work with

I skimmed through the paper on structured bindings here http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0144r0.pdf but I was not able to get a good sense of which types of structs the syntax allows binding to. My best guess is that the…
Curious
  • 20,870
  • 8
  • 61
  • 146
7
votes
1 answer

Structured binding for fixed-size span

After finding out you can ergonomically convert std::vectors into fix-sized std::spans, I thought I'd try out structured bindings for std::vector: auto _ = std::vector{ 1,2,3 }; std::span a = std::span(_).first<3>(); auto [b,c,d] = a; But…
Tom Huntington
  • 2,260
  • 10
  • 20
7
votes
1 answer

Why can't std::unique_ptr be returned after structured binding without using std::move?

When I try to compile the following code I get the error C2280. I guess the compiler is trying to copy the unique_ptr or something. #include std::pair> CreatePair() { std::unique_ptr my_int(new int); …
Olppah
  • 790
  • 1
  • 10
  • 21
7
votes
2 answers

Decltype of struct data members, using structured binding

I have a template taking a type that is structurally-bindable to two aliases (could be a tuple but also a struct). I need the type of those two variables the aliases point to. template T obj; // type of a/b in auto&& [a, b] = obj ? I…
Adam
  • 1,724
  • 4
  • 21
  • 31
7
votes
0 answers

lambda capturing structured bindings

As my understanding C++17 don't forbid lambda expressions from capturing structured bindings. the below is accepted by gcc but rejected by clang, yet cppreference claim that lambdas can't capture structured bindings. int arr[] {0, 1}; auto& [a, b] =…
Jans
  • 11,064
  • 3
  • 37
  • 45
7
votes
1 answer

Why does GCC diagnose a unused variable for structured bindings while Clang doesn't?

Let's start with a minimal example: #include int main() { auto [a, b] = std::pair(1, 'A'); return a; } Compiling with GCC 7.3 passing -std=c++17 and -Wunused-variable, and running it: : In function 'int…
Mário Feroldi
  • 3,463
  • 2
  • 24
  • 49
7
votes
2 answers

Why lambda expression's capture list cannot be decomposed using structured bindings

Before you throw a rotten tomato I know the practical application of lambda decomposition is currently limited as one wouldn't be able to find substitution-failure-friendly way to check the number of lambda captures hidden in decomposed variables.…
W.F.
  • 13,888
  • 2
  • 34
  • 81
7
votes
3 answers

Structured bindings width

Is it possible to determine how many variable names should I to specify in square brackets using structured bindings syntax to match the number of data members of a plain right hand side struct? I want to make a part of generic library, which uses…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
6
votes
1 answer

decltype does not preserve ref qualifier from structured binding

Usually decltype perseveres the ref qualifiers auto a = 0; auto& a_ref = a; static_assert(std::is_reference_v); But apparently not when it's argument is obtained from structured binding auto p = std::pair{1, 2.f}; auto& [i, d] =…
Tom Huntington
  • 2,260
  • 10
  • 20
6
votes
1 answer

Why does MSVC find ineligible get() in structured binding?

Consider the following code: #include #include #include #include template struct A { void get() {} // #1 }; template struct B : A... {}; template
El Mismo Sol
  • 869
  • 1
  • 6
  • 14
6
votes
2 answers

How to shadow existing variables when destructuring in C++?

Is there any way to shadow existing variables when destructuring a std::pair? For example if I have the following functions defined: #include #include std::pair returns_pair(int a, int b) { return std::make_pair(a…
ruohola
  • 21,987
  • 6
  • 62
  • 97
5
votes
2 answers

Tuple-like structured binding for an inner class of a class template

I want to provide structured binding for an inner class of a class template. How can I specialise std::tuple_size for that inner class? I can't use structured binding to data member, because the inner class may be a type incompatible with that…
Tarquiscani
  • 649
  • 7
  • 20