Skip to content

Commit eae83a4

Browse files
Add print and println
1 parent 94cc3c7 commit eae83a4

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

print/print.h

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Simple console printing routines.
2+
*
3+
* (C) Copyright Martin Williams 2018
4+
*
5+
* Inspired by code from C++ Templates The Complete Guide, Second Edition, Vandevoorde, Josuttis, Gregor.
6+
*
7+
* Usage:
8+
* print(arg1, arg2, arg3...) Print the list of arguments to standard output.
9+
*
10+
* println(arg1, arg2, arg3...) Print the list of arguments to standard output with a newline at the end.
11+
*/
12+
13+
#ifndef _PRINT_H_
14+
#define _PRINT_H_
15+
16+
#include <iostream>
17+
18+
19+
namespace misc {
20+
21+
template<typename Arg>
22+
void print(Arg arg)
23+
{
24+
std::cout << arg;
25+
}
26+
27+
28+
template<typename Arg, typename... Types>
29+
void print(Arg firstArg, Types... otherArgs)
30+
{
31+
print(firstArg);
32+
print(otherArgs...);
33+
}
34+
35+
36+
template<typename Arg>
37+
void println(Arg arg)
38+
{
39+
std::cout << arg << std::endl;
40+
}
41+
42+
43+
template<typename Arg, typename... Types>
44+
void println(Arg firstArg, Types... otherArgs)
45+
{
46+
print(firstArg);
47+
println(otherArgs...);
48+
}
49+
50+
}
51+
52+
#endif
53+

print/test/test.cxx

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "../print.h"
2+
3+
int main()
4+
{
5+
misc::println("Hello, world!");
6+
misc::println("Several", "strings", "combined");
7+
misc::println("A sequence of ints: ", 1, 2, 3, 4, 5);
8+
misc::println("A sequence of floats: ", 1.2, " ", 3.4, " ", 5.6, " ", 7.8, " ", 9.12, " ");
9+
10+
misc::print("Text", " on");
11+
misc::print(" the", " same");
12+
misc::println(" line");
13+
14+
return 0;
15+
}
16+

0 commit comments

Comments
 (0)