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
1
vote
1 answer

Structured binding with map in range-based loop captures by reference instead of value

In the code below I use auto (not auto&, const auto, const auto& or auto&&), but k has a reference type. Why is it captured by reference (as GCC says) and not by value? Looks counterintuitive. If it's captured by value (as Clang says), why it is…
4LegsDrivenCat
  • 1,247
  • 1
  • 15
  • 24
1
vote
0 answers

Combining function of array return type with structured binding declaration in C++17

I'm developing a system for an embedded application where performance is critical, but I also want well-structured code. I want to combine arrays (not lists) structured binding declaration but I have some issues. This is essentially what I want to…
Mathias
  • 308
  • 1
  • 9
1
vote
1 answer

Structure bindings for class data accessed through member functions

Stroustrup mentions in his book A Tour of C++ (section 3.6.3) that it is possible to do structured bindings for a class whose data is accessible through member functions. Here's the example code he gives: complex z = {1,2} auto [re,im] =…
Frank
  • 150
  • 6
1
vote
0 answers

Detect if class has method that is suitable for structured binding

Is there a way to write type trait or concept, that can be used to detect if type has a method that can be used in structured binding with particular types? Suppose Q is class in question. I want to get a compile time bool constant that tells me if…
1
vote
1 answer

"Merge" PODs into one

Is there a way to compose (or merge, aggregate) PODs between them ? One intuitive solution: struct Base1 { int i; }; struct Base2 { char c; }; struct Derived : Base1, Base2 {}; // in a more generalized way // template struct…
1
vote
1 answer

Moving from pair/tuple elements via structured binding

Given std::pair, std::set> p, what is the right syntax to move its elements via structured binding? How to do std::set first_set = std::move(p.first); std::set second_set = std::move(p.second); via structured binding…
user2052436
  • 4,321
  • 1
  • 25
  • 46
1
vote
1 answer

How to emulate structured binding init-capture in lambda C++?

Is there any equivalent to structured binding inside the init-capture list in lambda? I know this is invalid, but is there any way to declare 'i' and 's' without declaring outside of the lambda? std::pair p { 1, "two" }; auto f =…
Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
1
vote
2 answers

Unpacking array values to Class variables in Cpp

I want to unpack array values to different class variables but for that I am getting an error. auto [SendLowROS.motorCmd[FR_0].Kp, SendLowROS.motorCmd[FR_1].Kp, SendLowROS.motorCmd[FR_2].Kp, SendLowROS.motorCmd[FL_0].Kp,…
Tahir Mahmood
  • 305
  • 1
  • 11
1
vote
1 answer

C++ structured bindings with nested initializer list

Is there a way to write the following code nicely in C++20 without using std::array for (const auto [x, y, z] : {std::array{1, 2, 3}, {47, 48, 49}, {100, 200,…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
1 answer

Use structure binding when iterator a vector of std::pair*

the below code shows how a structure binding normally works. It provides a more descriptive name comparing to "first", "second". map sites; sites.insert({ "GeeksforGeeks", "Coding Resources" }); for (auto& [key, value] : sites) { …
Lin Paul
  • 49
  • 8
1
vote
0 answers

The type of identifier when using structured binding with 'tuple-like' binding protocol

I'm learning structured binding, I'm confused with the type of identifier about "tuple-like" binding protocol, for example: int a = 3; double b = 2.1; tuple tpl(a, b); auto [a1, b1] = tpl; auto& [a2, b2] = tpl; …
beyondV
  • 36
  • 2
1
vote
2 answers

Using Structured binding declaration in Range-based for loop

I am trying to combine two std::map containers (say std::map foo, bar) into a third std::map (say std::map foobar). I know that I can achieve this using iterators as below: std::map::iterator itr1 = foo.begin(); …
Nitin Singla
  • 106
  • 2
  • 9
1
vote
1 answer

Taking built-in types by value in a structured binding declaration in a range-for for std::map

I have: std::map map; for (auto&& [first, second] : map) { /* ... */ } where Foo is a class declared elsewhere There are 2 issues that I see as it stands: 1. Mapped type constness second here is Foo& which is correct, but you can also…
1
vote
1 answer

What is the best practice for performing structured bindings on read-only (i.e. const) accessors?

Consider the following class foo: class foo { private: int x = 0; int y = 0; public: template int const& get() const { if constexpr (I == 0) { return x; } else return y; …
ph3rin
  • 4,426
  • 1
  • 18
  • 42
1
vote
0 answers

Disable warning for unused structured bindings

I'm extracting a tuple with auto [...] but I'm only using some of the tuple coordinates. I wanted to know if there is some elegant way to avoid the unused-variable compiler warning? Here is my code. #include #include #include…