stdtuple

Creating a sub-tuple starting from a std::tuple<some_types…>

◇◆丶佛笑我妖孽 提交于 2019-12-23 12:31:23
问题 Let us suppose that a std::tuple<some_types...> is given. I would like to create a new std::tuple whose types are the ones indexed in [0, sizeof...(some_types) - 2] . For instance, let's suppose that the starting tuple is std::tuple<int, double, bool> . I would like to obtain a sub-tuple defined as std::tuple<int, double> . I'm quite new to variadic templates. As a first step I tried to write a struct in charge of storing the different types of the original std::tuple with the aim of creating

Why can't std::tuple be element-wise constructed with a std::tuple of compatible types?

和自甴很熟 提交于 2019-12-21 07:28:24
问题 I can't initialize std::tuple elements element-wise from a std::tuple of compatible types. Why doesn't it work as with boost::tuple ? #include <tuple> #include <boost/tuple/tuple.hpp> template <typename T> struct Foo { // error: cannot convert 'std::tuple<int>' to 'int' in initialization template <typename U> Foo(U &&u) : val(std::forward<U>(u)) {} T val; }; int main() { boost::tuple<Foo<int>>{boost::tuple<int>{}}; // ok auto a = boost::tuple<int>{}; boost::tuple<Foo<int>>{a}; // ok std:

Making `std::get` play nice with SFINAE

心不动则不痛 提交于 2019-12-20 17:41:28
问题 std::get does not seem to be SFINAE-friendly, as shown by the following test case: template <class T, class C> auto foo(C &c) -> decltype(std::get<T>(c)) { return std::get<T>(c); } template <class> void foo(...) { } int main() { std::tuple<int> tuple{42}; foo<int>(tuple); // Works fine foo<double>(tuple); // Crashes and burns } See it live on Coliru The goal is to divert the second call to foo towards the second overload. In practice, libstdc++ gives: /usr/local/bin/../lib/gcc/x86_64-pc-linux

Making `std::get` play nice with SFINAE

≯℡__Kan透↙ 提交于 2019-12-20 17:41:28
问题 std::get does not seem to be SFINAE-friendly, as shown by the following test case: template <class T, class C> auto foo(C &c) -> decltype(std::get<T>(c)) { return std::get<T>(c); } template <class> void foo(...) { } int main() { std::tuple<int> tuple{42}; foo<int>(tuple); // Works fine foo<double>(tuple); // Crashes and burns } See it live on Coliru The goal is to divert the second call to foo towards the second overload. In practice, libstdc++ gives: /usr/local/bin/../lib/gcc/x86_64-pc-linux

Why can't you assign a pair from a tuple, but tuple can be assigned from a pair?

♀尐吖头ヾ 提交于 2019-12-19 16:47:41
问题 I'm not clear why it is legal to assign tuple<X,Y>=pair<X,Y> But it is illegal to assign pair<X,Y>=tuple<X,Y> std::pair<int, double> x { 1 , 5.5}; std::tuple<int, double> y { 1 , 5.5}; int a; double b; std::tie(a,b) = x; std::tie(a,b) = y; x = y; // THIS LINE (line 12) y = x; // but this is fine ??? Shouldn't this be symmetrical? Using g++ 4.8.1 gives the following errors: tp.cpp:12:4: error: no match for operator= (operand types are std::pair<int, double> and std::tuple<int, double>) x = y;

How to perform tuple arithmetic in C++ (c++11/c++17)?

£可爱£侵袭症+ 提交于 2019-12-18 13:33:43
问题 I'm trying to write template functions/operators such as + for doing arithmetic operations between two tuples of the same type. For example, for std::tuple<int,double> t = std::make_tuple(1,2); I'd like to do auto t1 = t + t; The logic is simple: to do the arithmetic element-wise. But I can't figure out how to make this work in c++ template programming (c++11/17). My code below doesn't compile with g++ -std=c++11 tuple_arith.cpp . In particular, I can't figure out the right way of getting the

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

拈花ヽ惹草 提交于 2019-12-17 18:17:02
问题 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

How to compare tuples of different length?

对着背影说爱祢 提交于 2019-12-12 16:11:01
问题 I would like to write a comparator which compares tuples of different length but have the same "prefix". Consider following case, I have two tuples. auto t1 = std::make_tuple(10, "Test1"); auto t2 = std::make_tuple(10, "Test", 3.14); I would like to apply "less" for t1 < t2, where only two first members of tuple are compared (same type?) and the third one is just omited. Is it possible? 回答1: Well, since no one has chimed in, here is the solution. It uses C++14 std::index_sequence , so the

how to forward the types of tuple to specialize other template?

吃可爱长大的小学妹 提交于 2019-12-12 14:51:16
问题 currently I'm working on a dynamic container structure, which represents one pod value or has vector of pointers with same container type. The container has an interface optional<T> expect_value<T>() 。 For pod types the implemention is simple. For the non pod value, I would call expect_value<tuple<args...>>() , the args would be tuple as well. But when implement this function, I come across a trouble: how to redirect a.expect_value<tuple<args...>>() to a.expect_value_tuple<args...>>() . For

error: cannot pass objects of non-trivially-copyable type through `…`

陌路散爱 提交于 2019-12-12 01:36:37
问题 I have a class unit , which has the properties std::is_trivial<unit>::value; // true std::is_trivially_copyable<unit>::value; // true (on compilers which have this trait) I'd like to pass are vectors of unit as a tuple, e.g. using geodeticTuple = std::tuple<unit, unit, unit>; I need these vectors to be passable into conversion functions who use different types of arguments, e.g. someOtherType convert(const geodeticTuple& point, const geodeticTuple& origin) or someOtherType convert(const