forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
69 lines (56 loc) · 1.78 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
/***************************************************************************
* @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.53:
//! Write your own version of the print functions and test them by printing
//! one, two, and five arguments, each of which should have different types.
//!
//! Exercise 16.54:
//! What happens if we call print on a type that doesn’t have an << operator?
// didn't compile.
//!
//! Exercise 16.55:
//! Explain how the variadic version of print would execute if we declared
//! the nonvariadic version of print after the definition of the variadic
//! version.
// error: no matching function for call to 'print(std::ostream&)'
//!
#include <iostream>
class Foo
{};
//! 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...);
}
int main()
{
//! printing one argument
//print(std::cout, 1);
//! printing two arguments
print(std::cout, 1,2);
//! printing 5 arguments
//print(std::cout, 1,2,3,4,"sss");
//Foo foo,bar;
//print(std::cout,foo,bar);
}