structured-bindings

What are use cases for structured bindings?

霸气de小男生 提交于 2019-11-30 08:17:48
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 decomposition Let's declare a tuple: std::tuple<int, std::string> t(42, "foo"); Named elementwise copies may be easily obtained with structured bindings in one line: auto [i, s] = t; which is equivalent to: auto i = std::get<0>(t); auto s = std::get<1>(t); or int i; std::string s; std::tie(i, s) = t; References to tuple elements can also be obtained

structured bindings with std::minmax and rvalues

回眸只為那壹抹淺笑 提交于 2019-11-30 08:14:46
I ran into a rather subtle bug when using std::minmax with structured bindings. It appears that passed rvalues will not always be copied as one might expect. Originally I was using a T operator[]() const on a custom container, but it seems to be the same with a literal integer. #include <algorithm> #include <cstdio> #include <tuple> int main() { auto [amin, amax] = std::minmax(3, 6); printf("%d,%d\n", amin, amax); // undefined,undefined int bmin, bmax; std::tie(bmin, bmax) = std::minmax(3, 6); printf("%d,%d\n", bmin, bmax); // 3,6 } Using GCC 8.1.1 with -O1 -Wuninitialized will result in 0,0

Structured bindings width

不问归期 提交于 2019-11-29 07:14:24
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 structured bindings to decompose arbitrary classes into its constituents. At the moment there is no variadic version of structured bindings (and, I think, cannot be for current syntax proposed), but my first thought is to make a set of overloadings of some function decompose() , which performs decomposition of struct parameter into a set of its

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

狂风中的少年 提交于 2019-11-28 20:02:30
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] = minmax(data); Now that I learned "lists are written { like, this }" there comes a new list-syntax? Why? What is the problem with curly braces here? This is still under debate. It's difficult to be certain which syntax will be least confusing given how many uses there are for [] and {} already. There's also the risk that "least confusing" and "easiest to

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

岁酱吖の 提交于 2019-11-28 03:23:43
问题 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 isn't mentioned there, so I wonder if there's a known workaround that I'm just not thinking of. Consider this JSON-parsing code (which may contain other bugs; please ignore them for the purposes of this question): using Value = std::any; using String

Structured binding to replace std::tie abuse

不打扰是莪最后的温柔 提交于 2019-11-27 18:19:03
问题 In reading this summary of the c++17 final features I was a bit surprised by the section on structured bindings (emphasis mine): structured bindings Until now, there was a known trick to abuse std::tie to assign a tuple or pair to different variables directly, instead of having to deal with the result type manually. This was a hack , and also the variables had to exist, now you can declare the variables and initialize them in one line: auto [a , b , c] = getvalues(); The braces are needed,

structured binding with [[maybe_unused]]

▼魔方 西西 提交于 2019-11-27 14:17:17
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 name, but then we'll get warnings about unused variables. With the latest heads of both clang and gcc, this does the expected thing, which is nice and useful, [[maybe_unused]] auto x =4 ; // fine, no warning [[maybe_unused]] auto [a,dummyb,dummyc] = std::tuple<int,int,float>(1,1,1.0f); but I would also have hoped this would work: auto [g,[[maybe

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

穿精又带淫゛_ 提交于 2019-11-27 12:39:04
问题 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] = minmax(data); Now that I learned "lists are written { like, this }" there comes a new list-syntax? Why? What is the problem with curly braces here? 回答1: This is still under debate. It's difficult to be certain which syntax will be least confusing given

Lambda implicit capture fails with variable declared from structured binding

橙三吉。 提交于 2019-11-27 09:01:30
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 code compiles: [] { int a, b; std::tie(a, b) = [] {return std::make_tuple(1, 2); }(); auto r = [&] {return a; }(); }(); I thought that the two samples were equivalent. Is it a compiler bug or am I missing something ? Core issue 2313 changed the standard so that structured bindings are never names of variables, making them never capturable. P0588R1 's

structured bindings: when something looks like a reference and behaves similarly to a reference, but it's not a reference

守給你的承諾、 提交于 2019-11-27 05:00:29
Yesterday I've seen an interesting question here on SO about structured binding. We can sum up it as it follows. Consider the example code below: #include <tuple> #include <type_traits> int main() { auto tup = std::make_tuple(1, 2); auto & [ a, b ] = tup; // the following line won't compile for a isn't a reference // static_assert(std::is_reference_v<decltype(a)>); } In this case decltype(a) is int (probably) because of this bullet (working draft): if e is an unparenthesized id-expression naming a structured binding [...], decltype(e) is the referenced type as given in the specification of the