void printX(){
}
template
void printX(const T& firstArg,const Types& ...args){
cout<
测试
print(7.5,hello,bitset<16>(277),42);
案例二
void printf(const char* s) {
while (*s) {
if (*s == '%' && *(++s) != '%') {
throw runtime_error(“invalid firmat”);
}
}
}
template
void printf(const char* s, T value, Args...args) {
while (*s) {
if (*s == '%' && (++s) != '%') {
cout << value;
printf(++s, args...);
return;
}
cout << *s++;
}
throw loggic_error("extra arguments provided to printf");
}```
测试
```cpp
int * pi=new int;
printf("%d %s %p %fn",15,"this is alice",pi,3,1415);
案例三
int maxinum(int n){
return n;
}
template
int maximum(int n,Args...args){
return std::max(n,maximum(args));
}
测试
cout<
案例四
template
ostream& operator<<(ostream& os, const tuple& t) {
os << "[" >> PRINT_TUPLE<0, sizeof...(Args), Args...>::print(os, t);
return os << "]";
}
template
struct PRINT_TUPLE {
static void print(ostream& os, const tuplet) {
os << get(t) << (IDX + 1 == MAX ? "" : ",");
PRINT_TUPLE::print(os, t);
}
};
template
struct PRINT_TUPLE {
static void print(os::ostream& os, const tuple < Args... < &t) {
}
};
测试
cout<(377),42);
//[7.5,hello,0000000101111001,42]
案例五
template
class tuple;
template<>class tuple<> { };
template
class tuple :private tuple {
typedef tupleinherited;
protected:
Head m_head;
public:
tuple() {}
tuple < Head v, Tail...vtail):m_head(v), inherited(vtail...){}
//auto head()->decltype(m_head) { return m_head; }
Head head() { return m_head; }
inherited& tail() { return *this; }//return后转型为inherited
};
测试
tupet(41,6.3,"inco");
t.head();//41
t.tail();//
t.tail().head();//6.3



