stdtuple

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,

Difference between std::pair and std::tuple with only two members?

淺唱寂寞╮ 提交于 2019-11-27 11:37:23
问题 Is there a difference between an std::pair and an std::tuple with only two members? (Besides the obvious that std::pair requires two and only two members and tuple may have more or less...) 回答1: There are some differences: std::tuple can never be by standard-layout (at least, it's not required to be by the standard). Every std::pair<T, Y> is standard-layout if both T and Y are standard-layout. It's a bit easier to get the contents of a pair than a tuple . You have to use a function call in

Constructor arguments from tuple

纵饮孤独 提交于 2019-11-27 04:38:46
问题 Suppose I have a template which is parametrized by a class type and a number of argument types. a set of arguments matching these types are stored in a tuple. How can one pass these to a constructor of the class type? In almost C++11 code: template<typename T, typename... Args> struct foo { tuple<Args...> args; T gen() { return T(get<0>(args), get<1>(args), ...); } }; How can the ... in the constructor call be filled without fixing the length? I guess I could come up with some complicated

Why is it not good to use recursive inheritance for std::tuple implementations?

风流意气都作罢 提交于 2019-11-27 04:14:19
In this question, Howard Hinnant said Some implementations of std::tuple use recursive inheritance. But the good ones don't. ;-) Can someone please shed some light on that? A non-recursive implementation has better compile-time performance. Believe it or not, in a heavily used library facility like std::tuple , how it is implemented can impact (for better or worse), the compile times the client sees. Recursive implementations tend to produce compile times that are linear in the depth of recursion (or can be even worse). This impacts more than just the instantiation of the tuple itself. std:

Using tuple in unordered_map

自古美人都是妖i 提交于 2019-11-27 02:46:21
问题 I want to use tuple consisting of int , char , char in my unordered_map . I am doing like this: #include <string> #include <unordered_map> #include <cstring> #include <iostream> #include <tuple> using namespace std; tuple <int,char,char> kk; unordered_map<kk,int> map; int main() { map[1,"c","b"]=23; return 0; } but this gives me following errors: map.cpp:9:21: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Key, class _Tp, class _Hash, class _Pred,

std::tuple_element need deep template instantination

放肆的年华 提交于 2019-11-26 23:43:18
问题 in here http://en.cppreference.com/w/cpp/utility/tuple/tuple_element given possible implementation of std::tuple_element. template< std::size_t I, class T > struct tuple_element; // recursive case template< std::size_t I, class Head, class... Tail > struct tuple_element<I, std::tuple<Head, Tail...>> : std::tuple_element<I-1, std::tuple<Tail...>> { }; // base case template< class Head, class... Tail > struct tuple_element<0, std::tuple<Head, Tail...>> { typedef Head type; }; But, this

Why is it not good to use recursive inheritance for std::tuple implementations?

感情迁移 提交于 2019-11-26 11:05:54
问题 In this question, Howard Hinnant said Some implementations of std::tuple use recursive inheritance. But the good ones don\'t. ;-) Can someone please shed some light on that? 回答1: A non-recursive implementation has better compile-time performance. Believe it or not, in a heavily used library facility like std::tuple , how it is implemented can impact (for better or worse), the compile times the client sees. Recursive implementations tend to produce compile times that are linear in the depth of

How can you iterate over the elements of an std::tuple?

泄露秘密 提交于 2019-11-25 22:25:55
问题 How can I iterate over a tuple (using C++11)? I tried the following: for(int i=0; i<std::tuple_size<T...>::value; ++i) std::get<i>(my_tuple).do_sth(); but this doesn\'t work: Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a fixed-length argument list. Error 2: i cannot appear in a constant expression. So, how do I correctly iterate over the elements of a tuple? 回答1: Boost.Fusion is a possibility: Untested example: struct DoSomething { template<typename T> void operator()(T&