-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
variadic2.cpp
34 lines (29 loc) · 807 Bytes
/
variadic2.cpp
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
//
// Created by light on 19-11-4.
//
#include <iostream>
using namespace std;
void print(const char *s) {
while (*s) {
if (*s == '%' && *(++s) != '%')
throw std::runtime_error("invalid format string: missing arguments");
std::cout << *s++;
}
}
template<typename T, typename... Args>
void print(const char *s, T value, Args... args) {
while (*s) {
if (*s == '%' && *(++s) != '%') {
std::cout << value;
print(++s, args...); // 即便当 *s == 0 也会产生调用,以检测更多的类型参数。
return;
}
std::cout << *s++;
}
throw std::logic_error("extra arguments provided to printf");
}
int main() {
int *pi = new int;
print("%d %s %p %f\n", 15, "Acc", pi, 3.14);
return 0;
}