1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| void func(){} template<typename T, typename... Type> void func(const T& firstArg, const Type&... args) { func(args...); }
void printX(){ }
template<typename T, typename... Types> void printX(const T& firstArg, const Type&...args){ cout << firstArg << endl; printX(args...); }
template<typename... Type> void printX(const Type&... args){}
print(7.5, "hello",bitset<16>(377),42);
int* pi = new int; printf("%d %s %p %f\n", 15, "this is Ace", pi, 3.1415926);
template<typename T, typename... Args> void printf(const char* s, T value, Args... args){ while(*s){ if(*s=='%' && *(++s)!='%'){ std::cout << value; printf(++s, args...); return; } std::cout << *s++; } throw std::logic_error("extra argments provided to print"); }
void printf(const char* s){ while(*s){ if(*s=='%' && *(++s)!='%') throw std::runtime_error("invalid format string: missing argment"); std::cout << *s++; } }
int maximum(int n){ return n; }
template<typename... Args> int maximum(int n, Args... args){ return std::max(n, maximum(args...)); }
cout << make_tuple(7.5,string("hello"),bitset<16>(377),42)
template<typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t){ os<<"["; PRINT_TUPLE<0,sizeof...(Args),Args...>::print(os,t); return os << "]"; }
template<int IDX, int MAX, typename... Args> struct PRINT_TUPLE{ static void print(ostream& os, const tuple<Args...>& t){ os << get<IDX>(t) << (IDX+1==MAX?"":","); PRINT_TUPLE<IDX,MAX,Args...>::print(os,t); } } template<int MAX, typename... Args> struct PRINT_TUPLE<IDX,MAX,Args...>{ static void print (std::ostream& os, const tuple<Args...>& t){ }; }
template<typename... Values> class tuple; template<> class tuple(){ };
template<typename Head, typename... Tail> class tuple<Head,Tail...> : private tuple<Tail...>{ typedef tuple<Tail...> inherited; public: tuple(){} tuple(Head v, Tail... vtail) : m_head(v), inherited(vtail...) { }
typename Head::type head() {return m_head;} inherited& tail() {return *this;} protected: Head m_head; }
tuple<int, float, string> t(41, 6.3, "nico");
template<typename... Values> class tuple; template<> class tuple(){ };
template<typename Head, typename... Tail> class tuple<Head,Tail...> : private tuple<Tail...>{ typedef tuple<Tail...> inherited; protected: Head m_head; public: tuple(){} tuple(Head v, Tail... vtail) : m_head(v), inherited(vtail...) { }
auto head() -> decltype(m_head) {return m_head;} inherited& tail() {return *this;} }
template<typename... Values> class tuple; template<> class tuple(){ };
template<typename Head, typename... Tail> class tuple<Head,Tail...> : private tuple<Tail...>{ typedef tuple<Tail...> inherited; public: tuple(){} tuple(Head v, Tail... vtail) : m_head(v), inherited(vtail...) { }
Head head(){return m_head;} inherited& tail() {return *this;} protected: Head m_head; }
template<typename... Values> class tup; template<> class tup<> {}; template<typename Head, typename... Tail> class tup<Head, Tail...> { typedef tup<Tail...> composited; protected: composited m_tail; Head m_head; public: tup(){} tup(Head v, Tail... vtail) : m_tail(vtail...), m_head(v){}
Head head() {return m_head;} composited& tail() {return m_tail;} }
|