structured-bindings

What are use cases for structured bindings?

萝らか妹 提交于 2019-12-18 12:50:08
问题 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

Variables marked as const using structured bindings are not const

坚强是说给别人听的谎言 提交于 2019-12-18 10:58:20
问题 I have been writing a set of classes to allow for a simple python-like zip -function. The following snippet works (almost) just as expected. However, the two variables a and b are not const . std::vector<double> v1{0.0, 1.1, 2.2, 3.3}; std::vector<int> v2{0, 1, 2}; for (auto const& [a, b] : zip(v1, v2)) { std::cout << a << '\t' << b << std::endl; a = 3; // I expected this to give a compiler error, but it does not std::cout << a << '\t' << b << std::endl; } I have been using gcc 7.3.0. Here is

If structured bindings cannot be constexpr why can they be used in constexpr function?

我们两清 提交于 2019-12-08 15:13:20
问题 According to this answer apparently there is no good reason why structured bindings are not allowed to be constexpr, yet the standard still forbids it. In this case, however, shouldn't the use of the structured bindings inside the constexpr function also be prohibited? Consider a simple snippet: #include <utility> constexpr int foo(std::pair<int, int> p) { auto [a, b] = p; return a; } int main() { constexpr int a = foo({1, 2}); static_assert(a == 1); } Both gcc and clang does not cause

Why does including <utility> break structured bindings in GCC?

空扰寡人 提交于 2019-12-05 12:27:10
问题 Consider: struct Point { int x, y; }; int main() { const auto [x, y] = Point{}; } This code compiles fine with gcc 7.1 in C++17 mode, however this one: #include <utility> struct Point { int x, y; }; int main() { const auto [x, y] = Point{}; } gives an error: bug.cpp: In function 'int main()': bug.cpp:7:16: error: 'std::tuple_size<const Point>::value' is not an integral constant expression const auto [x, y] = Point{}; ^~~~~~ What's going on here? A compiler bug, or is this how structured

Is decltype(auto) for a structured binding supposed to be a reference?

纵饮孤独 提交于 2019-12-05 08:51:08
问题 Consider an example: #include <iostream> #include <type_traits> #include <tuple> int main() { auto tup = std::make_tuple(1, 2); auto [ a, b ] = tup; decltype(auto) e = a; std::cout << std::boolalpha << std::is_reference_v<decltype(e)> << std::endl; } clang (output: false ) and gcc (output: true ) are disagreeing in this simple case. Having in mind e.g. this Q&As should the e be a reference or is it a gcc bug? Or maybe the code is ill-formed? 回答1: The identifers themselves are references. From

Why can't decomposition declarations be constexpr?

余生长醉 提交于 2019-12-05 08:27:13
问题 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'

Shall structured binding to a copy of a const c-array be const?

我怕爱的太早我们不能终老 提交于 2019-12-04 09:57:32
问题 Consider this code (demo): #include <tuple> #include <type_traits> struct Ag{int i;int j;}; using T = std::tuple<int,int>; using Ar = int[2]; const Ag ag {}; const T t {}; const Ar ar {}; void bind_ag(){ auto [i,j] = ag; static_assert(std::is_same_v<decltype((i)),int&>); } void bind_t(){ auto [i,j] = t; static_assert(std::is_same_v<decltype((i)),int&>); } void bind_ar(){ auto [i,j] = ar; static_assert(std::is_same_v<decltype((i)),int&>); //For GCC static_assert(std::is_same_v<decltype((i))

Why does including <utility> break structured bindings in GCC?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 23:55:02
Consider: struct Point { int x, y; }; int main() { const auto [x, y] = Point{}; } This code compiles fine with gcc 7.1 in C++17 mode, however this one: #include <utility> struct Point { int x, y; }; int main() { const auto [x, y] = Point{}; } gives an error: bug.cpp: In function 'int main()': bug.cpp:7:16: error: 'std::tuple_size<const Point>::value' is not an integral constant expression const auto [x, y] = Point{}; ^~~~~~ What's going on here? A compiler bug, or is this how structured bindings are supposed to work? Barry This is compiler bug 78939 . Although it's a bit more complicated than

Is decltype(auto) for a structured binding supposed to be a reference?

落花浮王杯 提交于 2019-12-03 23:33:16
Consider an example: #include <iostream> #include <type_traits> #include <tuple> int main() { auto tup = std::make_tuple(1, 2); auto [ a, b ] = tup; decltype(auto) e = a; std::cout << std::boolalpha << std::is_reference_v<decltype(e)> << std::endl; } clang (output: false ) and gcc (output: true ) are disagreeing in this simple case. Having in mind e.g. this Q&As should the e be a reference or is it a gcc bug? Or maybe the code is ill-formed? The identifers themselves are references. From [dcl.struct.bind]/3 : Given the type T i designated by std​::​tuple_­element<i, E>​::​type , each v i is a

Do const references in structured bindings extend the lifetime of the decomposed object?

一世执手 提交于 2019-12-03 23:33:11
Does writing const auto& [a, b] = f(); guarantee extending the lifetime of the object returned from f() , or at least the objects a and b are bound to? Reading through the proposal I don't see anything obvious in the language to make me sure that it does unless it's just covered by something else. However, the following doesn't extend the lifetime of the temporary, so I don't see how it would be covered: const auto& a = std::get<0>(f()); At the top of the paper it seems to suggest that it is covered the cv-qualifiers and ref-qualifier of the decomposition declaration are applied to the