stdtuple

How to get reference to an element of a std::tuple?

白昼怎懂夜的黑 提交于 2019-12-01 01:16:13
问题 You can get the value of the n th element of an std::tuple using std::get<n>(tuple) . But I need to pass one element of that tuple as reference to a function. How do I get the reference to an element of a std::tuple ? 回答1: std::get returns a reference(either const or non-const), so this works: void fun(int &a) { a = 15; } void test() { std::tuple<int, char> foo{ 12, 'a' }; fun(std::get<0>(foo)); } Demo here. 回答2: get returns a reference, rvalue reference or const reference depending on the

How to get N-th type from a tuple?

前提是你 提交于 2019-11-30 17:35:39
I want to make a template where I can input an index and it will give me the type at that index. I know I can do this with decltype(std::get<N>(tup)) but I would like to implement this myself. For example, I would like to do this, typename get<N, std::tuple<int, bool, std::string>>::type; ...and it will give me the type at position N - 1 (because arrays indexed starting from 0). How can I do this? Thanks. You can use a class template and partial specializations to do what you want. (Note that std::tuple_element does almost the same like the other answer says): #include <tuple> #include <type

STL-pair-like triplet class - do I roll my own?

点点圈 提交于 2019-11-30 17:03:09
I want to use a triplet class, as similar as possible to std::pair. STL doesn't seem to have one. I don't want to use something too heavy, like Boost. Is there some useful FOSS non-restrictive-license triplet class I could lift from somewhere? Should I roll my own? Should I do something else entirely? Edit: About std::tuple ... Is there really no benefit to a triplet-specific class? I mean, with tuple, I can't do template<typename T1, typename T2, typename T3> std::tuple<T1, T2, T3> triple; now can I? Won't I have to typedef individual-type-combination triples? Joseph Mansfield No, don't roll

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

Is using `std::get<I>` on a `std::tuple` guaranteed to be thread-safe for different values of `I`?

倖福魔咒の 提交于 2019-11-30 13:39:07
问题 Let's say I have std::tuple<T0, T1, T2> my_tuple{x0, x1, x2}; where T0 , T1 and T2 are value types (i.e. no aliasing is possible) . Is it safe to access my_tuple 's elements and mutate them concurrently from multiple threads using std::get , as long as every thread accesses a different element? Example: template <typename T> void process(T& x) { /* mutate `x` */ } // ... std::thread{[&]{ process(std::get<0>(my_tuple)); }}.detach(); std::thread{[&]{ process(std::get<1>(my_tuple)); }}.detach();

Optimal way to access std::tuple element in runtime by index

南楼画角 提交于 2019-11-30 12:11:28
问题 I have function at designed to access std::tuple element by index specified in runtime template<std::size_t _Index = 0, typename _Tuple, typename _Function> inline typename std::enable_if<_Index == std::tuple_size<_Tuple>::value, void>::type for_each(_Tuple &, _Function) {} template<std::size_t _Index = 0, typename _Tuple, typename _Function> inline typename std::enable_if < _Index < std::tuple_size<_Tuple>::value, void>::type for_each(_Tuple &t, _Function f) { f(std::get<_Index>(t)); for

Is using `std::get<I>` on a `std::tuple` guaranteed to be thread-safe for different values of `I`?

谁说我不能喝 提交于 2019-11-30 08:07:39
Let's say I have std::tuple<T0, T1, T2> my_tuple{x0, x1, x2}; where T0 , T1 and T2 are value types (i.e. no aliasing is possible) . Is it safe to access my_tuple 's elements and mutate them concurrently from multiple threads using std::get , as long as every thread accesses a different element? Example: template <typename T> void process(T& x) { /* mutate `x` */ } // ... std::thread{[&]{ process(std::get<0>(my_tuple)); }}.detach(); std::thread{[&]{ process(std::get<1>(my_tuple)); }}.detach(); std::thread{[&]{ process(std::get<2>(my_tuple)); }}.detach(); Instinctively I would say it is safe, as

C++ std::tuple order of destruction

倖福魔咒の 提交于 2019-11-30 07:44:03
问题 Is there a rule which states in which order the members of an std::tuple are destroyed? For example if Function1 returns an std::tuple<std::unique_ptr<ClassA>, std::unique_ptr<ClassB>> to Function2 , then can I be sure that (when the scope of Function2 is left) the instance of ClassB referred to by the second member is destroyed before the instance of ClassA referred to by the first member? std::tuple< std::unique_ptr< ClassA >, std::unique_ptr< ClassB > > Function1() { std::tuple< std:

Optimal way to access std::tuple element in runtime by index

て烟熏妆下的殇ゞ 提交于 2019-11-30 02:09:24
I have function at designed to access std::tuple element by index specified in runtime template<std::size_t _Index = 0, typename _Tuple, typename _Function> inline typename std::enable_if<_Index == std::tuple_size<_Tuple>::value, void>::type for_each(_Tuple &, _Function) {} template<std::size_t _Index = 0, typename _Tuple, typename _Function> inline typename std::enable_if < _Index < std::tuple_size<_Tuple>::value, void>::type for_each(_Tuple &t, _Function f) { f(std::get<_Index>(t)); for_each<_Index + 1, _Tuple, _Function>(t, f); } namespace detail { namespace at { template < typename

How to get N-th type from a tuple?

大兔子大兔子 提交于 2019-11-30 00:50:56
问题 I want to make a template where I can input an index and it will give me the type at that index. I know I can do this with decltype(std::get<N>(tup)) but I would like to implement this myself. For example, I would like to do this, typename get<N, std::tuple<int, bool, std::string>>::type; ...and it will give me the type at position N - 1 (because arrays indexed starting from 0). How can I do this? Thanks. 回答1: You can use a class template and partial specializations to do what you want. (Note