Skip to content

Commit 106cc33

Browse files
committed
Updating to C++23
1 parent 8c571bc commit 106cc33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+232
-172
lines changed

headers/01-hellow.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// 01-hellow.cpp : prints a line of text to the console
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
int main() {
7-
cout << "Hello, World!" << '\n';
7+
println("Hello, World!");
88
}

headers/01-title.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// 01-title.cpp : output the title page of a well-known book
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
int main() {
7-
cout << 1+R"(
7+
print(1+R"(
88
Alice's
99
Adventures In
1010
Wonderland
1111
1212
by
1313
LEWIS CARROLL
14-
)";
14+
)");
1515
}

headers/02-assign.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// 02-assign.cpp : assign to local variables
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
int main() {
77
int i = 1, j = 2;
88
unsigned k;
9-
cout << "(1) i = " << i << ", j = " << j << ", k = " << k << '\n';
9+
println("(1) i = {}, j = {}, k = {}", i, j, k);
1010
i = j;
1111
j = 3;
1212
k = -1;
13-
cout << "(2) i = " << i << ", j = " << j << ", k = " << k << '\n';
13+
println("(2) i = {}, j = {}, k = {}", i, j, k);
1414
}

headers/02-constants.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// 02-constants.cpp : introducing the const keyword
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
const double PI = 3.14159265358979;
77

88
int main() {
99
auto const APPROX_E = 3;
10-
cout << "pi is almost exactly " << PI
11-
<< "e is approximately " << APPROX_E
12-
<< '\n';
10+
println("pi is almost exactly {}, while e is approximately {}",
11+
PI, APPROX_E);
1312
}

headers/02-constexpr.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
// 02-constexpr.cpp : introducing the constexpr keyword
22

3-
#include <iostream>
3+
#include <print>
44
#include <cmath>
55
using namespace std;
66

7-
const double PI1 = acos(-1.0); // acos is not (yet) constexpr
7+
constexpr double PI1 = acos(-1.0);
88
constexpr double PI2 = 22.0 / 7.0;
99

10-
// the following line does not compile and has been commented out
11-
//static_assert(PI1 > 3.141 && PI1 < 3.143);
10+
static_assert(PI1 > 3.141 && PI1 < 3.143);
1211
static_assert(PI2 > 3.141 && PI2 < 3.143);
1312

1413
int main() {
15-
cout << "PI1 = " << PI1 << '\n';
16-
cout << "PI2 = " << PI2 << '\n';
14+
println("PI1 = {}", PI1);
15+
println("PI2 = {}", PI2);
1716
}

headers/02-height.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 02-height.cpp : define the same variable name in two different namespaces
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
namespace Wonderland {
@@ -12,9 +12,7 @@ namespace VictorianEngland {
1212
}
1313

1414
int main() {
15-
cout << "Alice\'s height varies between "
16-
<< Wonderland::alice_height_m
17-
<< "m and "
18-
<< VictorianEngland::alice_height_m
19-
<< "m.\n";
15+
println("Alice\'s height varies between {}m and {}m",
16+
Wonderland::alice_height_m,
17+
VictorianEngland::alice_height_m);
2018
}

headers/02-references.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// 02-references.cpp : introducing l-value references
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
int alice_age{ 9 };
77

88
int main() {
9-
cout << "Alice\'s age is " << alice_age << '\n';
9+
println("Alice\'s age is {}", alice_age);
1010
int& alice_age_ref = alice_age;
1111
alice_age_ref = 10;
12-
cout << "Alice\'s age is now " << alice_age << '\n';
12+
println("Alice\'s age is now {}", alice_age);
1313
}

headers/02-scopes.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// 02-scopes.cpp : define three variables with the same name in one program
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
auto a{ 1.5f };
77

88
int main() {
9-
cout << "(1) " << a << '\n';
9+
println("(1) {}", a);
1010
auto a{ 2u };
11-
cout << "(2) " << a << '\n';
11+
println("(2) {}", a);
1212
{
1313
auto a{ 2.5 };
14-
cout << "(3) " << a << '\n';
14+
println("(3) {}", a);
1515
}
1616
}

headers/02-swap.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// 02-swap.cpp : attempt to swap the values of an int and a double
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
int main() {
77
int a = 1;
88
double b = 2.5;
9-
cout << "(1) a = " << a << ", b = " << b << '\n';
9+
println("(1) a = {}, b = {}", a, b);
1010
a = 2.5;
1111
b = 1;
12-
cout << "(2) a = " << a << ", b = " << b << '\n';
12+
println("(2) a = {}, b = {}", a, b);
1313
}

headers/02-uniform.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// 02-uniform.cpp : avoid compiler error with uniform initialization and explicit narrowing cast
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
int main() {
77
// int c = { 2.5 }; // Error: this does NOT compile
88
int c = { static_cast<int>(2.5) }; // while this does
99
double d = { 1 }; // and so does this
10-
cout << "c = " << c << ", d = " << d << '\n';
10+
println("c = {}, d = {}", c, d);
1111
}

headers/03-calc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using namespace std;
66
int main() {
77
int r{}, x{}, y{};
88
char op{};
9-
cout << "Please enter a calulation (number op number, op is one of +-*/):\n";
9+
cout << "Please enter a calculation (number op number, op is one of +-*/):\n";
1010
cin >> x >> op >> y;
1111
switch (op) {
1212
case '+':

headers/04-inline.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 04-inline.cpp : use of an inline function
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
inline void swap(int& x, int& y) {
@@ -11,7 +11,7 @@ inline void swap(int& x, int& y) {
1111

1212
int main() {
1313
int a = 1, b = 2;
14-
cout << "(1) a = " << a << ", b = " << b << '\n';
14+
println("(1) a = {}, b = {}", a, b);
1515
swap(a, b);
16-
cout << "(2) a = " << a << ", b = " << b << '\n';
16+
println("(2) a = {}, b = {}", a, b);
1717
}

headers/04-noexcept.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// 04-noexcept.cpp : a noexcept function throwing an exception
22

3-
#include <iostream>
3+
#include <print>
44
#include <stdexcept>
55
using namespace std;
66

77
int throw_if_zero(int i) noexcept {
88
if (!i) {
99
throw runtime_error("found a zero");
1010
}
11-
cout << "throw_if_zero(): " << i << '\n';
11+
println("throw_if_zero(): {}", i);
1212
}
1313

1414
int main() {
@@ -18,7 +18,7 @@ int main() {
1818
throw_if_zero(0);
1919
}
2020
catch(...) {
21-
cout << "Caught an exception!\n";
21+
println("Caught an exception!");
2222
}
23-
cout << "Leaving main()\n";
23+
println("Leaving main()\n");
2424
}

headers/04-static-var.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// 04-static-var.cpp : preserving function state in a static variable
22

3-
#include <iostream>
3+
#include <print>
44
using namespace std;
55

66
void f() {
77
static int s{1};
8-
cout << s << '\n';
8+
println("{}", s);
99
++s;
1010
}
1111

headers/06-pixel1.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ string_view get_color(Color c) {
1919
switch (c) {
2020
case Color::red:
2121
return "red";
22-
break;
2322
case Color::green:
2423
return "green";
25-
break;
2624
case Color::blue:
2725
return "blue";
28-
break;
26+
default:
27+
return "<no color>";
2928
}
30-
return "<no color>";
3129
}
3230

3331
int main() {

headers/06-pixel2.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ string_view get_color(Color c) {
1818
switch (c) {
1919
case Color::red:
2020
return "red";
21-
break;
2221
case Color::green:
2322
return "green";
24-
break;
2523
case Color::blue:
2624
return "blue";
27-
break;
25+
default:
26+
return "<no color>";
2827
}
29-
return "<no color>";
3028
}
3129

3230
int main() {

headers/06-point3.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Point{
77
int x{}, y{};
88
};
99

10-
Point operator+ (const Point& lhs, const Point& rhs) {
10+
const Point operator+ (const Point& lhs, const Point& rhs) {
1111
Point result;
1212
result.x = lhs.x + rhs.x;
1313
result.y = lhs.y + rhs.y;

headers/06-point4.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Point{
1313
}
1414
};
1515

16-
Point operator+ (const Point& lhs, const Point& rhs) { // non-member operator+
16+
const Point operator+ (const Point& lhs, const Point& rhs) { // non-member operator+
1717
Point result{ lhs };
1818
result += rhs;
1919
return result;

headers/08-format1.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// 08-format1.cpp : Basic usage of format string
2+
3+
#include <print>
4+
#include <string>
5+
using namespace std;
6+
7+
int main() {
8+
string s{ "Formatted" };
9+
auto d{ 10.0 / 3.0 };
10+
auto i{ 20000 };
11+
println("{0:20}:{2:8}, {1:12.11}", s, d, i);
12+
}

headers/08-format2.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 08-format2.cpp : Various format string-using functions
2+
3+
#include <print>
4+
#include <format>
5+
#include <string>
6+
#include <iostream>
7+
#include <iterator>
8+
#include <array>
9+
#include <cmath>
10+
using namespace std;
11+
12+
int main() {
13+
string world{ "World" };
14+
print(cout, "Hello, {}!\n", world);
15+
println("{1} or {0}", false, true);
16+
17+
constexpr const char *fmt = "Approximation of π = {:.12g}";
18+
string s = format(fmt, asin(1.0) * 2);
19+
cout << s << '\n';
20+
21+
constexpr const wchar_t *wfmt = L"Approximation of pi = {:.12g}";
22+
wstring ws = format(wfmt, asin(1.0) * 2);
23+
wcout << ws << L'\n';
24+
25+
format_to(ostream_iterator<char>(cout), "Hello, {}!\n", world);
26+
wstring ww{ L"World" };
27+
array<wchar_t,9> wa;
28+
auto iter = format_to_n(wa.begin(), 8, L"Hello, {}!\n", ww);
29+
*(iter.out) = L'\0';
30+
wcout << wa.data() << L'\n';
31+
}

headers/09-person1.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
// 09-person1.cpp : model Person as a class with constructor
22

3+
#include <chrono>
34
#include <iostream>
45
#include <string>
56
#include <string_view>
67
using namespace std;
7-
8-
struct Date {
9-
int year{}, month{}, day{};
10-
};
8+
using namespace std::chrono;
119

1210
class Person {
1311
public:
14-
Person(const Date& dob, string_view familyname, string_view firstname)
12+
Person(const year_month_day& dob, string_view familyname, string_view firstname)
1513
: dob{ dob }, familyname{ familyname }, firstname{ firstname }
1614
{}
1715
string getName() const { return firstname + ' ' + familyname; }
16+
const year_month_day& getDob() const { return dob; }
1817
private:
19-
const Date dob;
18+
const year_month_day dob;
2019
string familyname, firstname;
2120
};
2221

2322

2423
int main() {
25-
Person genius{ { 1879, 3, 14 }, "Einstein", "Albert" };
26-
cout << genius.getName() << '\n';
24+
Person genius{ { 1879y, March, 14d }, "Einstein", "Albert" };
25+
cout << genius.getName() << " was born " << genius.getDob() << '\n';
2726
}

0 commit comments

Comments
 (0)