Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
My Tuple class
#include <iostream> struct Nil{}; template<typename T1 = Nil, typename T2 = Nil, typename T3 = Nil, typename T4 = Nil> struct Tuple : Tuple<T2,T3,T4> { T1 x; using Base = Tuple<T2,T3,T4>; Base* base() { return static_cast<Base*>(this);} Tuple(const T1& t1 = T1(), const T2& t2 = T2(), const T3& t3 = T3(), const T4& t4 = T4()) : Base(t2,t3,t4), x(t1){} }; template<typename T1> struct Tuple<T1> { T1 x; Tuple(const T1& t1= T1(), const Nil& = Nil(), const Nil& = Nil(), const Nil& = Nil()): x(t1){}; }; template<> struct Tuple<> { Tuple(){}; }; template<typename T1, typename T2, typename T3, typename T4> void print_elements(std::ostream& os, Tuple<T1, T2, T3, T4>& t) { os << t.x <<", "; print_elements(os, *t.base()); } template<typename T1> void print_elements(std::ostream& os, Tuple<T1>& t) { os << t.x; } template<typename T1, typename T2, typename T3, typename T4> std::ostream& operator << ( std::ostream& os, Tuple<T1,T2,T3,T4>& t) { os << "{"; print_elements(os,t); os << "}"; return os; } template<int N, typename T1, typename T2, typename T3, typename T4> struct sel; template<typename T1, typename T2, typename T3, typename T4> struct sel<0, T1, T2, T3,T4>{ using type = T1;}; template<typename T1, typename T2, typename T3, typename T4> struct sel<1, T1, T2, T3,T4>{ using type = T2;}; template<typename T1, typename T2, typename T3, typename T4> struct sel<2, T1, T2, T3,T4>{ using type = T3;}; template<typename T1, typename T2, typename T3, typename T4> struct sel<3, T1, T2, T3,T4>{ using type = T4;}; template<int N, typename T1, typename T2, typename T3, typename T4> using Select = typename sel<N, T1,T2,T3,T4>::type; template<typename Ret, int N> struct SNthTuple { template<typename Tup> static Ret& get(Tup& t) { return SNthTuple<Ret,N-1>::get(*t.base()); } }; template<typename Ret> struct SNthTuple<Ret,0> { template<typename Tup> static Ret& get(Tup& t) { return t.x; } }; template<int N, typename T1, typename T2, typename T3, typename T4> Select<N,T1,T2,T3,T4>& get(Tuple<T1,T2,T3,T4>& t) { return SNthTuple<Select<N,T1,T2,T3,T4>,N>::get(t); }; int main() { Tuple<int,double> t1(1,2.9); Tuple<double, Tuple<int,double>, int, bool> t2(1.2, Tuple<int,double>(1,2), 1, false) ; std::cout << get<0>(t2) << std::endl; std::cout << get<1>(t2) << std::endl; std::cout << get<2>(t2) << std::endl; std::cout << get<3>(t2) << std::endl; std::cout << t2 << std::endl; }
run
|
edit
|
history
|
help
0
Set of intervals.
PriQTel2
code
MyStack
Breakfast Function
Operator Overload
aa
Policy based smart pointer
Member function detection
PayTax