vararg.cpp
832 Bytes
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
#include <iostream>
#include <cstdarg>
#include <sstream>
using namespace std;
extern void error (int ...);
const char *Null_cp = 0;
int
main (int argc, char *argv[])
{
switch (argc)
{
case 1:
error (0, argv[0], Null_cp);
break;
case 2:
error (0, argv[0], argv[1], Null_cp);
break;
default:
ostringstream s;
s << argc-1;
string ss=s.str();
error (1, argv[0], "with", ss.c_str(), "arguments",
Null_cp);
}
//...
}
void
error (int severity ...) // "severity" followed by a zeroterminated list of char*s
{
va_list ap;
va_start (ap, severity); // arg startup
for (;;)
{
char *p = va_arg (ap, char *);
if (p == 0)
break;
cerr << p << ' ';
}
va_end (ap); // arg cleanup
cerr << '\n';
if (severity)
exit (severity);
}