forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
112 lines (91 loc) · 2.81 KB
/
main.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
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
/***************************************************************************
* @file main.cpp
* @author Alan.W
* @date 16 Feb 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//!
//! Exercise 16.56:
//! Write and test a variadic version of errorMsg.
//!
//! Exercise 16.57:
//! Compare your variadic version of errorMsg to the error_msg function in
//! § 6.2.6 (p. 220). What are the advantages and disadvantages of each
//! approach?
// The error_msg takes initializer_list as the argument. So only the elements
// stored in it must be the same or at least convertible. In contrast, the variadic
// version provides better flexibility.
//!
#include <iostream>
#include <memory>
#include <sstream>
//! always declare first:
template <typename T> std::string debug_rep(const T& t);
template <typename T> std::string debug_rep(T* p);
std::string debug_rep(const std::string &s);
std::string debug_rep(char* p);
std::string debug_rep(const char *p);
//! print any type we don't otherwise.
template<typename T> std::string debug_rep(const T& t)
{
std::ostringstream ret;
ret << t;
return ret.str();
}
//! print pointers as their pointer value, followed by the object to which the pointer points
template<typename T> std::string debug_rep(T* p)
{
std::ostringstream ret;
ret << "pointer: " << p;
if(p)
ret << " " << debug_rep(*p);
else
ret << " null pointer";
return ret.str();
}
//! non-template version
std::string debug_rep(const std::string &s)
{
return '"' + s + '"';
}
//! convert the character pointers to string and call the string version of debug_rep
std::string debug_rep(char *p)
{
return debug_rep(std::string(p));
}
std::string debug_rep(const char *p)
{
return debug_rep(std::string(p));
}
//! function to end the recursion and print the last element
//! this function must be declared before the variadic version of
//! print is defined
template<typename T>
std::ostream& print(std::ostream& os, const T& t)
{
return os << t;
//! ^
//! note: no seperator after the last element in the pack
}
//! this version of print will be called for all but the last element in the pack
template<typename T, typename... Args>
std::ostream&
print(std::ostream &os, const T &t, const Args&... rest)
{
//! print the first argument
os << t << ",";
//! recursive call; print the other arguments
return print(os,rest...);
}
//! call debug_rep on each argument in the call to print
template<typename... Args>
std::ostream& errorMsg(std::ostream& os, const Args... rest)
{
//! print(os,debug_rep(rest)...);
return print(os,debug_rep(rest)...);
}
int main()
{
errorMsg(std::cout, 1,2,3,4,9.0f,"sss","alan");
}