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

How to check whether a name is an alias or a reference in C++17?

From cppref Like a reference, a structured binding is an alias to an existing object. Unlike a reference, the type of a structured binding does not have to be a reference type. For example: int a[2] = { 1, 2 }; auto [x, y] = a; x and y are…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
1
vote
1 answer

How to correctly forward structured binding arguments in a macro

I would like to write a macro that can take an arbitrary number of variable bindings, followed by a single expression, and rewrite those as a lambda function. The goal is to see if it's possible to implement something like the concise lambda syntax…
Mononofu
  • 902
  • 7
  • 15
1
vote
1 answer

Failing to work around g++ 7.1 structured binding bug with Boost.Bimap

In my project I am using Boost.Bimap to implement bidirectional maps. Look at this very simple MCVE on godbolt, where I am using structured binding to print the key-value pair of the right map (which, according to the documentation, is…
andreee
  • 4,459
  • 22
  • 42
1
vote
3 answers

Name alias references for pair or tuple values

When restructuring some code I came across a 'problem' when returning a struct with 2 values. Now these really should be named for the documented effect. Later on I wanted to use tie so i changed the struct into inheriting from std::pair and just…
darune
  • 10,480
  • 2
  • 24
  • 62
1
vote
1 answer

Allow structured bindings for templated class

So I have a little class where I would like to add structured binding support. However I cannot figure out how to specialize std::tuple_element and std::tuple_size with my templated class. This is my attempt: template struct…
0
votes
1 answer

Why does optimizing with std::forward_as_tuple produce runtime errors?

Let's consider the following program: #include #include using namespace std; const int* f() { static int i = 5; return &i; } int main() { auto [a] = std::forward_as_tuple( f() ); //auto&& [a] =…
0
votes
3 answers

How can I use C++17 structured bindings with OpenCV Vec?

I would like to unpack the members of a cv::Vec4f into its components. cv::Vec4f line{0,1,2,3}; // What I currently have to do: float x1 = line[0]; float y1 = line[1]; float x2 = line[2]; float y2 = line[3]; // What I would like to be able to…
0
votes
0 answers

Difference between auto& and auto&& in structured binding used in range-based for loop

(Please let me know if this is a duplicate. I tried to search previous questions but couldn't find the same one.) When structured binding is used in a range-based for loop like this: for (auto & [k, v] : myMap) {...} or for (auto && [k, v] : myMap)…
starriet
  • 2,565
  • 22
  • 23
0
votes
1 answer

Structured bindings in foreach loop

Consider fallowing peace of code: using trading_day = std::pair; using fun_intersection = vector>; double stock_trading_simulation(const fun_vals& day_value, fun_intersection& trade_days, int base_stock_amount = 1000) { …
Michał Turek
  • 701
  • 3
  • 21
0
votes
1 answer

Function as template argument that has a different structure depending on method

I am writing a routine to find the numerical roots of a function in C++. Depending on the algorithm, I can supply either the function or both the function and derivative. For example, I have two separate routines template
0
votes
1 answer

invalid initializer for structured binding declaration

How should I fix this error? Should I use a const std::tuple instead? constexpr auto [ Y_AxisLen, X_AxisLen, fillCharacter ] { 36, 168, ' ' }; This gives errors like: error: structured binding declaration cannot be 'constexpr' 434…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
0
votes
0 answers

Structured binding argument deduction fails

I am trying to expand this answer to return the result of the function in addition to the CPU time. Here is my code: #include #include #include template auto MeasureFunctionCpuTime(F…
0
votes
1 answer

Java 8+ Unpacking Primitives From Singleton Objects Using Lambda or Stream? Structured Binding in Java?

Effectively I need to take a subset of data out of a function return, but I don't actually need the returned object. So, is there a way to simply take what I need with a Stream or a Lambda-like syntax? And yes, I understand you CAN use…
patrickjp93
  • 399
  • 4
  • 20
0
votes
2 answers

Accessing references to existing object in a range based for loop with structured binding

I am struggling to find an easy solution to alter some already existing objects. Lets assume I have the following pairs std::pair p1 = {1,foo()}; std::pair p2 = {2,foo()}; std::pair p3 = {3,foo()}; with foo being a…
0
votes
1 answer

c++17 language extension and std:c++17 flag

I'm confused by different errors I have in Visual Studio 2017 (version 15.9.11): 'if constexpr' is a C++17 language extension and language feature 'structured bindings' requires compiler flag '/std:c++17' I know that adding /std:c++17 flag will…
1 2 3
9
10