variadic template(二)
作者:互联网
重写 print 函数
#include<iostream> using namespace std; void printfX(const char *s) { while (*s) { if (*s == '%' && *(++s) != '%') throw "invalid format string: missing arguments"; std::cout << *s++; } } template<typename T, typename... Args> void printfX(const char* s, T value, Args... args) { while (*s) { if (*s == '%' && *(++s) != '%') { std::cout << value; printfX(s, args...); // call even when *s == 0 to detect extra arguments return; } std::cout << *s++; } throw "extra arguments provided to printf"; } int main() { int* pi = new int; printfX("%d %s %p %f\n",15,"This is Ace",pi,3.1415926); return 0; }
标签:std,const,cout,void,printfX,char,template,variadic 来源: https://www.cnblogs.com/wanghao-boke/p/15854004.html