stdtuple

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

三世轮回 提交于 2019-11-30 00:15:00
问题 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;

Why is std::pair faster than std::tuple

时光总嘲笑我的痴心妄想 提交于 2019-11-29 20:46:09
Here is the code for testing. Tuple test: using namespace std; int main(){ vector<tuple<int,int>> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_tuple(var, var)); } } Pair test: #include <vector> using namespace std; int main(){ vector<pair<int,int>> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_pair(var, var)); } } I did the time measurement via Linux time command. The results are: | | -O0 | -O2 | |:------|:-------:|:--------:| | Pair | 8.9 s | 1.60 s | | Tuple | 19.8 s | 1.96 s | I am wondering, why is such a big difference between those two data structures

Tuple isn't being constructed in order?

爱⌒轻易说出口 提交于 2019-11-29 10:32:12
The following program : #include <iostream> #include <tuple> struct A { A() { std::cout << "A constructor\n"; } }; struct B { B() { std::cout << "B constructor\n"; } }; int main() { std::tuple<A, B> t; } gives different outputs on different compilers: # libstdc++ B constructor A constructor # libc++ A constructor B constructor This seem weird... I figured the standard would have guaranteed the tuple elements be constructed in-order, e.g., A, B, ..., Y, Z? std::tuple construction order is currently unspecified . A proposal for a concrete decision on its order has been submitted to the committee

C++ std::tuple order of destruction

送分小仙女□ 提交于 2019-11-29 05:23:58
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::unique_ptr< ClassA >, std::unique_ptr< ClassB > > garbage; get<0>(garbage).reset( /* ... */ ); get<1>

Why is std::pair faster than std::tuple

可紊 提交于 2019-11-28 16:26:07
问题 Here is the code for testing. Tuple test: using namespace std; int main(){ vector<tuple<int,int>> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_tuple(var, var)); } } Pair test: #include <vector> using namespace std; int main(){ vector<pair<int,int>> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_pair(var, var)); } } I did the time measurement via Linux time command. The results are: | | -O0 | -O2 | |:------|:-------:|:--------:| | Pair | 8.9 s | 1.60 s | |

Using tuple in unordered_map

二次信任 提交于 2019-11-28 09:07:25
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, class _Alloc> class std::unordered_map’ map.cpp:9:21: error: expected a type, got ‘kk’ map.cpp:9:21: error

Is returning a 2-tuple less efficient than std::pair?

限于喜欢 提交于 2019-11-28 06:52:37
Consider this code: #include <utility> #include <tuple> std::pair<int, int> f1() { return std::make_pair(0x111, 0x222); } std::tuple<int, int> f2() { return std::make_tuple(0x111, 0x222); } Clang 3 and 4 generate similar code for both on x86-64: f1(): movabs rax,0x22200000111 ret f2(): movabs rax,0x11100000222 ; opposite packing order, not important ret But Clang 5 generates different code for f2() : f2(): movabs rax,0x11100000222 mov QWORD PTR [rdi],rax mov rax,rdi ret As do GCC 4 through GCC 7: f2(): movabs rdx,0x11100000222 mov rax,rdi mov QWORD PTR [rdi],rdx ; GCC 4-6 use 2 DWORD stores

Tuple isn't being constructed in order?

穿精又带淫゛_ 提交于 2019-11-28 03:35:48
问题 The following program: #include <iostream> #include <tuple> struct A { A() { std::cout << "A constructor\n"; } }; struct B { B() { std::cout << "B constructor\n"; } }; int main() { std::tuple<A, B> t; } gives different outputs on different compilers: # libstdc++ B constructor A constructor # libc++ A constructor B constructor This seem weird... I figured the standard would have guaranteed the tuple elements be constructed in-order, e.g., A, B, ..., Y, Z? 回答1: std::tuple construction order is

std::tuple_element need deep template instantination

余生长醉 提交于 2019-11-28 02:23:55
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 implementation need deep recursion instantiation, if tuple has a lot parameters ( more that 100 or 200

Constructor arguments from tuple

喜欢而已 提交于 2019-11-27 23:15:10
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 mechanism of recursive template calls which does this, but I can't believe that I'm the first to want this