structured-bindings

Why can't decomposition declarations be constexpr?

好久不见. 提交于 2019-12-03 22:03:15
Consider the following snippet to test the upcoming C++17 feature decomposition declarations (formerly known as structured bindings) #include <cassert> #include <utility> constexpr auto divmod(int n, int d) { return std::make_pair(n / d, n % d); // in g++7, also just std::pair{n/d, n%d} } int main() { constexpr auto [q, r] = divmod(10, 3); static_assert(q == 3 && r ==1); } This fails on both g++7-SVN and clang-4.0-SVN with the message: decomposition declaration cannot be declared 'constexpr' Dropping the constexpr definition and changing to a regular assert() works on both compilers. None of

What are the types of identifiers introduced by structured bindings in C++17?

前提是你 提交于 2019-12-01 16:56:00
To my knowledge, identifiers introduced by structured bindings in C++17 are in fact references to some "hidden" variable. Such that auto [ a, b ] = std::make_tuple(1, 2); is kind-of equivalent to auto e = std::make_tuple(1, 2); auto& a = std::get<0>(e); auto& b = std::get<1>(e); However, if I print out std::is_reference<decltype(a)>::value , I get 0 in the first case 1 in the second. Why is that? if I print out std::is_reference<decltype(a)>::value , I get 0 in the first case 1 in the second. Why is that even if we can prove that a and b refer to the elements in the tuple and one can modify

Understand structured binding in C++17 by analogy

孤人 提交于 2019-12-01 15:54:17
I'm trying to understand structured binding introduced in C++17. The explanation on cppreference is not obvious to me, but it looks like cv-auto ref-operator [x, y, z] = ... is roughly equivalent to (not to consider array case) cv-auto ref-operator unique_name = ... #define x unique_name.member_a #define y unique_name.member_b #define z unique_name.member_c The key point here is that x y z are not independently defined variables, but just aliases of the return value members. And cv-auto ref-operator applies to the return value, not the aliases (the syntax may be misleading here). For instance,

Structured binding and tie()

人走茶凉 提交于 2019-12-01 15:43:52
Given these declarations: int a[3] {10,20,30}; std::tuple<int,int,int> b {11,22,33}; I can use structured binding declarations to decode a and b : auto [x1,y1,z1] = a; auto [x2,y2,z2] = b; But if x1 , y1 , etc. already exist, what do I do? std::tie(x1,y1,z1) = a; // ERROR std::tie(x2,y2,z2) = b; // OK This works for b but not for a . Is there a similar simple construct that works for a , or do I have to fetch a[0] , a[1] and a[2] separately? Nope. Structured bindings has specific language rules to handle arrays and certain other types. tie() is specifically a tuple<T&...> and can only be

What are the types of identifiers introduced by structured bindings in C++17?

梦想的初衷 提交于 2019-12-01 14:57:34
问题 To my knowledge, identifiers introduced by structured bindings in C++17 are in fact references to some "hidden" variable. Such that auto [ a, b ] = std::make_tuple(1, 2); is kind-of equivalent to auto e = std::make_tuple(1, 2); auto& a = std::get<0>(e); auto& b = std::get<1>(e); However, if I print out std::is_reference<decltype(a)>::value , I get 0 in the first case 1 in the second. Why is that? 回答1: if I print out std::is_reference<decltype(a)>::value , I get 0 in the first case 1 in the

Structured binding and tie()

喜夏-厌秋 提交于 2019-12-01 14:38:10
问题 Given these declarations: int a[3] {10,20,30}; std::tuple<int,int,int> b {11,22,33}; I can use structured binding declarations to decode a and b : auto [x1,y1,z1] = a; auto [x2,y2,z2] = b; But if x1 , y1 , etc. already exist, what do I do? std::tie(x1,y1,z1) = a; // ERROR std::tie(x2,y2,z2) = b; // OK This works for b but not for a . Is there a similar simple construct that works for a , or do I have to fetch a[0] , a[1] and a[2] separately? 回答1: Nope. Structured bindings has specific

Are nested structured bindings possible?

房东的猫 提交于 2019-12-01 13:41:43
问题 Assume I have an object of type std::map<std::string, std::tuple<int, float>> data; Is it possible to access the element types in a nested way (i.e. when used in ranged for loop) like this for (auto [str, [my_int, my_float]] : data) /* do something */ 回答1: No, it is not possible. I distinctly remember reading somewhere that nested structured bindings are not allowed for C++17, but they are considering allowing it in a future standard. Can't find the source though. 回答2: No, they aren't

structured bindings and range-based-for; supress unused warning in gcc

我的未来我决定 提交于 2019-11-30 18:56:10
I want to traverse a map using structure bindings, ignoring the key: for (auto& [unused, val] : my_map) do_something(val); I have tried different options with gcc-7.2.0: // The warning is issued for ([[maybe_unused]] auto& [unused, val] : my_map) do_something(val); // Syntax error for (auto& [[[maybe_unused]] unused, val] : my_map) do_something(val); // The same two combinations above with [[gnu::unused]]. It seems that the [[maybe_unused]] attribute is not implemented yet for structure bindings. Is there any simple solution to this? Any macro, gcc/gnu extension, or any pragma to temporarily

Structured binding to replace std::tie abuse

*爱你&永不变心* 提交于 2019-11-30 13:56:05
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, getvalues returns a tuple. std::pair is not mentioned in the proposal, so its unclear if this works with

structured bindings with std::minmax and rvalues

孤人 提交于 2019-11-30 13:48:20
问题 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)