structured-bindings

Providing tuple-like structured binding access for a class

安稳与你 提交于 2021-02-18 21:12:24
问题 I'm trying to support tuple-like structured binding access for a class. For simplicity, I'll use the following class in the rest of this post: struct Test { int v = 42; }; (I'm aware that this class supports structured bindings out of the box but let's assume it does not.) To enable tuple-like access to the member of Test , we must specialize std::tuple_size and std::tuple_element : namespace std { template<> struct tuple_size<Test> { static const std::size_t value = 1; }; template<std::size

Unpacking variadic tuples in c++17

戏子无情 提交于 2021-02-04 17:27:50
问题 Is there anything better in c++17 (maybe C++2a) than the classic C++14 way to unpack variadic tuple with std::index_sequence? Anything better than this: template <typename ...I> class MultiIterator { public: MultiIterator(I const& ...i) : i(i...) {} MultiIterator& operator ++() { increment(std::index_sequence_for<I...>{}); return *this; } private: template <std::size_t ...C> void increment(std::index_sequence<C...>) { std::ignore = std::make_tuple(++std::get<C>(i)...); } std::tuple<I...> i; }

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

馋奶兔 提交于 2020-01-01 07:43:08
问题 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

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

眉间皱痕 提交于 2019-12-30 06:12:53
问题 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

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

回眸只為那壹抹淺笑 提交于 2019-12-30 06:12:32
问题 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

Structured bindings width

▼魔方 西西 提交于 2019-12-29 06:59:06
问题 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

Understand structured binding in C++17 by analogy

梦想的初衷 提交于 2019-12-19 17:13:45
问题 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

Understand structured binding in C++17 by analogy

一曲冷凌霜 提交于 2019-12-19 17:13:10
问题 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

What are use cases for structured bindings?

天大地大妈咪最大 提交于 2019-12-18 12:52:19
问题 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