Skip to content

Commit

Permalink
Updating to C++23
Browse files Browse the repository at this point in the history
  • Loading branch information
cpp-tutor committed Jan 21, 2024
1 parent 8c571bc commit 106cc33
Show file tree
Hide file tree
Showing 46 changed files with 232 additions and 172 deletions.
4 changes: 2 additions & 2 deletions headers/01-hellow.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 01-hellow.cpp : prints a line of text to the console

#include <iostream>
#include <print>
using namespace std;

int main() {
cout << "Hello, World!" << '\n';
println("Hello, World!");
}
6 changes: 3 additions & 3 deletions headers/01-title.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// 01-title.cpp : output the title page of a well-known book

#include <iostream>
#include <print>
using namespace std;

int main() {
cout << 1+R"(
print(1+R"(
Alice's
Adventures In
Wonderland
by
LEWIS CARROLL
)";
)");
}
6 changes: 3 additions & 3 deletions headers/02-assign.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// 02-assign.cpp : assign to local variables

#include <iostream>
#include <print>
using namespace std;

int main() {
int i = 1, j = 2;
unsigned k;
cout << "(1) i = " << i << ", j = " << j << ", k = " << k << '\n';
println("(1) i = {}, j = {}, k = {}", i, j, k);
i = j;
j = 3;
k = -1;
cout << "(2) i = " << i << ", j = " << j << ", k = " << k << '\n';
println("(2) i = {}, j = {}, k = {}", i, j, k);
}
7 changes: 3 additions & 4 deletions headers/02-constants.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// 02-constants.cpp : introducing the const keyword

#include <iostream>
#include <print>
using namespace std;

const double PI = 3.14159265358979;

int main() {
auto const APPROX_E = 3;
cout << "pi is almost exactly " << PI
<< "e is approximately " << APPROX_E
<< '\n';
println("pi is almost exactly {}, while e is approximately {}",
PI, APPROX_E);
}
11 changes: 5 additions & 6 deletions headers/02-constexpr.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// 02-constexpr.cpp : introducing the constexpr keyword

#include <iostream>
#include <print>
#include <cmath>
using namespace std;

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

// the following line does not compile and has been commented out
//static_assert(PI1 > 3.141 && PI1 < 3.143);
static_assert(PI1 > 3.141 && PI1 < 3.143);
static_assert(PI2 > 3.141 && PI2 < 3.143);

int main() {
cout << "PI1 = " << PI1 << '\n';
cout << "PI2 = " << PI2 << '\n';
println("PI1 = {}", PI1);
println("PI2 = {}", PI2);
}
10 changes: 4 additions & 6 deletions headers/02-height.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 02-height.cpp : define the same variable name in two different namespaces

#include <iostream>
#include <print>
using namespace std;

namespace Wonderland {
Expand All @@ -12,9 +12,7 @@ namespace VictorianEngland {
}

int main() {
cout << "Alice\'s height varies between "
<< Wonderland::alice_height_m
<< "m and "
<< VictorianEngland::alice_height_m
<< "m.\n";
println("Alice\'s height varies between {}m and {}m",
Wonderland::alice_height_m,
VictorianEngland::alice_height_m);
}
6 changes: 3 additions & 3 deletions headers/02-references.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// 02-references.cpp : introducing l-value references

#include <iostream>
#include <print>
using namespace std;

int alice_age{ 9 };

int main() {
cout << "Alice\'s age is " << alice_age << '\n';
println("Alice\'s age is {}", alice_age);
int& alice_age_ref = alice_age;
alice_age_ref = 10;
cout << "Alice\'s age is now " << alice_age << '\n';
println("Alice\'s age is now {}", alice_age);
}
8 changes: 4 additions & 4 deletions headers/02-scopes.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// 02-scopes.cpp : define three variables with the same name in one program

#include <iostream>
#include <print>
using namespace std;

auto a{ 1.5f };

int main() {
cout << "(1) " << a << '\n';
println("(1) {}", a);
auto a{ 2u };
cout << "(2) " << a << '\n';
println("(2) {}", a);
{
auto a{ 2.5 };
cout << "(3) " << a << '\n';
println("(3) {}", a);
}
}
6 changes: 3 additions & 3 deletions headers/02-swap.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// 02-swap.cpp : attempt to swap the values of an int and a double

#include <iostream>
#include <print>
using namespace std;

int main() {
int a = 1;
double b = 2.5;
cout << "(1) a = " << a << ", b = " << b << '\n';
println("(1) a = {}, b = {}", a, b);
a = 2.5;
b = 1;
cout << "(2) a = " << a << ", b = " << b << '\n';
println("(2) a = {}, b = {}", a, b);
}
4 changes: 2 additions & 2 deletions headers/02-uniform.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// 02-uniform.cpp : avoid compiler error with uniform initialization and explicit narrowing cast

#include <iostream>
#include <print>
using namespace std;

int main() {
// int c = { 2.5 }; // Error: this does NOT compile
int c = { static_cast<int>(2.5) }; // while this does
double d = { 1 }; // and so does this
cout << "c = " << c << ", d = " << d << '\n';
println("c = {}, d = {}", c, d);
}
2 changes: 1 addition & 1 deletion headers/03-calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace std;
int main() {
int r{}, x{}, y{};
char op{};
cout << "Please enter a calulation (number op number, op is one of +-*/):\n";
cout << "Please enter a calculation (number op number, op is one of +-*/):\n";
cin >> x >> op >> y;
switch (op) {
case '+':
Expand Down
6 changes: 3 additions & 3 deletions headers/04-inline.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 04-inline.cpp : use of an inline function

#include <iostream>
#include <print>
using namespace std;

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

int main() {
int a = 1, b = 2;
cout << "(1) a = " << a << ", b = " << b << '\n';
println("(1) a = {}, b = {}", a, b);
swap(a, b);
cout << "(2) a = " << a << ", b = " << b << '\n';
println("(2) a = {}, b = {}", a, b);
}
8 changes: 4 additions & 4 deletions headers/04-noexcept.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// 04-noexcept.cpp : a noexcept function throwing an exception

#include <iostream>
#include <print>
#include <stdexcept>
using namespace std;

int throw_if_zero(int i) noexcept {
if (!i) {
throw runtime_error("found a zero");
}
cout << "throw_if_zero(): " << i << '\n';
println("throw_if_zero(): {}", i);
}

int main() {
Expand All @@ -18,7 +18,7 @@ int main() {
throw_if_zero(0);
}
catch(...) {
cout << "Caught an exception!\n";
println("Caught an exception!");
}
cout << "Leaving main()\n";
println("Leaving main()\n");
}
4 changes: 2 additions & 2 deletions headers/04-static-var.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// 04-static-var.cpp : preserving function state in a static variable

#include <iostream>
#include <print>
using namespace std;

void f() {
static int s{1};
cout << s << '\n';
println("{}", s);
++s;
}

Expand Down
6 changes: 2 additions & 4 deletions headers/06-pixel1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ string_view get_color(Color c) {
switch (c) {
case Color::red:
return "red";
break;
case Color::green:
return "green";
break;
case Color::blue:
return "blue";
break;
default:
return "<no color>";
}
return "<no color>";
}

int main() {
Expand Down
6 changes: 2 additions & 4 deletions headers/06-pixel2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ string_view get_color(Color c) {
switch (c) {
case Color::red:
return "red";
break;
case Color::green:
return "green";
break;
case Color::blue:
return "blue";
break;
default:
return "<no color>";
}
return "<no color>";
}

int main() {
Expand Down
2 changes: 1 addition & 1 deletion headers/06-point3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Point{
int x{}, y{};
};

Point operator+ (const Point& lhs, const Point& rhs) {
const Point operator+ (const Point& lhs, const Point& rhs) {
Point result;
result.x = lhs.x + rhs.x;
result.y = lhs.y + rhs.y;
Expand Down
2 changes: 1 addition & 1 deletion headers/06-point4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Point{
}
};

Point operator+ (const Point& lhs, const Point& rhs) { // non-member operator+
const Point operator+ (const Point& lhs, const Point& rhs) { // non-member operator+
Point result{ lhs };
result += rhs;
return result;
Expand Down
12 changes: 12 additions & 0 deletions headers/08-format1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 08-format1.cpp : Basic usage of format string

#include <print>
#include <string>
using namespace std;

int main() {
string s{ "Formatted" };
auto d{ 10.0 / 3.0 };
auto i{ 20000 };
println("{0:20}:{2:8}, {1:12.11}", s, d, i);
}
31 changes: 31 additions & 0 deletions headers/08-format2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 08-format2.cpp : Various format string-using functions

#include <print>
#include <format>
#include <string>
#include <iostream>
#include <iterator>
#include <array>
#include <cmath>
using namespace std;

int main() {
string world{ "World" };
print(cout, "Hello, {}!\n", world);
println("{1} or {0}", false, true);

constexpr const char *fmt = "Approximation of π = {:.12g}";
string s = format(fmt, asin(1.0) * 2);
cout << s << '\n';

constexpr const wchar_t *wfmt = L"Approximation of pi = {:.12g}";
wstring ws = format(wfmt, asin(1.0) * 2);
wcout << ws << L'\n';

format_to(ostream_iterator<char>(cout), "Hello, {}!\n", world);
wstring ww{ L"World" };
array<wchar_t,9> wa;
auto iter = format_to_n(wa.begin(), 8, L"Hello, {}!\n", ww);
*(iter.out) = L'\0';
wcout << wa.data() << L'\n';
}
15 changes: 7 additions & 8 deletions headers/09-person1.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
// 09-person1.cpp : model Person as a class with constructor

#include <chrono>
#include <iostream>
#include <string>
#include <string_view>
using namespace std;

struct Date {
int year{}, month{}, day{};
};
using namespace std::chrono;

class Person {
public:
Person(const Date& dob, string_view familyname, string_view firstname)
Person(const year_month_day& dob, string_view familyname, string_view firstname)
: dob{ dob }, familyname{ familyname }, firstname{ firstname }
{}
string getName() const { return firstname + ' ' + familyname; }
const year_month_day& getDob() const { return dob; }
private:
const Date dob;
const year_month_day dob;
string familyname, firstname;
};


int main() {
Person genius{ { 1879, 3, 14 }, "Einstein", "Albert" };
cout << genius.getName() << '\n';
Person genius{ { 1879y, March, 14d }, "Einstein", "Albert" };
cout << genius.getName() << " was born " << genius.getDob() << '\n';
}
Loading

0 comments on commit 106cc33

Please sign in to comment.