Skip to content

Commit 970ccf5

Browse files
authored
Merge pull request #678 from ckormanyos/modernization
2 parents dd9c1a1 + e740ad0 commit 970ccf5

38 files changed

Lines changed: 738 additions & 340 deletions

File tree

code_snippets/chapter05/chapter05.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ $GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion
5050
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_10-003_const_eval_inner_product.cpp -o ./bin/chapter05_10-003_const_eval_inner_product.exe
5151
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_11-001_tuple_things_simple.cpp -o ./bin/chapter05_11-001_tuple_things_simple.exe
5252
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_11-002_tuple_things_metaprogram.cpp -o ./bin/chapter05_11-002_tuple_things_metaprogram.exe
53-
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_11-003_tuple_things_metaprogram.cpp -o ./bin/chapter05_11-003_tuple_things_metaprogram.exe
53+
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_11-003_tuple_things_metaprog_alias.cpp -o ./bin/chapter05_11-003_tuple_things_metaprog_alias.exe
5454
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_12-001_prime_number.cpp -o ./bin/chapter05_12-001_prime_number.exe
5555
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_12-002_constants.cpp -o ./bin/chapter05_12-002_constants.exe
5656
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_12-003_stirlings_approx.cpp -o ./bin/chapter05_12-003_stirlings_approx.exe
57-
$GCC -std=c++20 -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_12-004_stirlings_approx_cpp_numbers.cpp -o ./bin/chapter05_12-004_stirlings_approx_cpp_numbers.exe
57+
$GCC -std=c++20 -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_12-004_stirlings_approx_numbers.cpp -o ./bin/chapter05_12-004_stirlings_approx_numbers.exe
58+
$GCC -std=c++20 -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_13-001_integer_sequence.cpp -o ./bin/chapter05_13-001_integer_sequence.exe
59+
$GCC -std=c++20 -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_13-002_array_to_tuple.cpp -o ./bin/chapter05_13-002_array_to_tuple.exe
60+
$GCC -std=c++20 -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_13-003_exp_series.cpp -o ./bin/chapter05_13-003_exp_series.exe
61+
$GCC -std=c++20 -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter05_14-001_stirlings_approx_complex.cpp -o ./bin/chapter05_14-001_stirlings_approx_complex.exe
5862

5963
ls -la \
6064
./bin/chapter05_01-001_template_add.exe \
@@ -83,11 +87,15 @@ ls -la \
8387
./bin/chapter05_10-003_const_eval_inner_product.exe \
8488
./bin/chapter05_11-001_tuple_things_simple.exe \
8589
./bin/chapter05_11-002_tuple_things_metaprogram.exe \
86-
./bin/chapter05_11-003_tuple_things_metaprogram.exe \
90+
./bin/chapter05_11-003_tuple_things_metaprog_alias.exe \
8791
./bin/chapter05_12-001_prime_number.exe \
8892
./bin/chapter05_12-002_constants.exe \
8993
./bin/chapter05_12-003_stirlings_approx.exe \
90-
./bin/chapter05_12-004_stirlings_approx_cpp_numbers.exe
94+
./bin/chapter05_12-004_stirlings_approx_numbers.exe \
95+
./bin/chapter05_13-001_integer_sequence.exe \
96+
./bin/chapter05_13-002_array_to_tuple.exe \
97+
./bin/chapter05_13-003_exp_series.exe \
98+
./bin/chapter05_14-001_stirlings_approx_complex.exe
9199

92100
result_ls=$?
93101

code_snippets/chapter05/chapter05_01-001_template_add.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019- 2023.
2+
// Copyright Christopher Kormanyos 2019- 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -15,17 +15,22 @@
1515
template<typename T>
1616
constexpr auto add(const T& a, const T& b) -> T
1717
{
18-
return T(a + b);
18+
return a + b;
1919
}
2020

21-
const auto n = add(1, 2); // 3
21+
// 3
22+
const auto n { add(1, 2) };
2223

23-
const auto s = add(std::string("abc"), std::string("xyz")); // "abcxyz".
24+
// "abcxyz".
25+
const auto str { add(std::string("abc"), std::string("xyz")) };
2426

2527
auto main() -> int;
2628

2729
auto main() -> int
2830
{
31+
// 3
2932
std::cout << n << std::endl;
30-
std::cout << s << std::endl;
33+
34+
// abcxyz
35+
std::cout << str << std::endl;
3136
}

code_snippets/chapter05/chapter05_01-002_template_add2.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019- 2020.
2+
// Copyright Christopher Kormanyos 2019- 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -11,12 +11,14 @@
1111

1212
template<typename T1,
1313
typename T2>
14-
auto add(const T1& a, const T2& b) -> T1
14+
constexpr
15+
auto add(const T1& a, const T2& b) noexcept -> T1
1516
{
1617
return a + T1(b);
1718
}
1819

19-
const auto d = add(1.1, 2); // OK, 3.1, double
20+
// OK, 3.1, double
21+
const double d { add(1.1, 2) };
2022

2123
auto main() -> int;
2224

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019- 2020.
2+
// Copyright Christopher Kormanyos 2019- 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -10,6 +10,7 @@
1010
#include <cstdint>
1111
#include <iomanip>
1212
#include <iostream>
13+
#include <sstream>
1314

1415
#define MAKE_WORD(lo, hi) \
1516
(uint16_t) (((uint16_t) (hi) << 8) | (lo))
@@ -20,24 +21,27 @@
2021
#define MAKE_QWORD(lo, hi) \
2122
(uint64_t) (((uint64_t) (hi) << 32) | (lo))
2223

23-
int main()
24+
auto main() -> int;
25+
26+
auto main() -> int
2427
{
25-
std::uint8_t lo8 = UINT8_C(0x34);
26-
std::uint8_t hi8 = UINT8_C(0x12);
27-
std::uint16_t u16
28-
= MAKE_WORD(lo8, hi8);
29-
30-
std::uint16_t lo16 = UINT16_C(0x5678);
31-
std::uint16_t hi16 = UINT16_C(0x1234);
32-
std::uint32_t u32
33-
= MAKE_DWORD(lo16, hi16);
34-
35-
std::uint32_t lo32 = UINT32_C(0x9ABC'DEF0);
36-
std::uint32_t hi32 = UINT32_C(0x1234'5678);
37-
std::uint64_t u64
38-
= MAKE_QWORD(lo32, hi32);
39-
40-
std::cout << std::hex << std::showbase << std::uppercase << u16 << std::endl;
41-
std::cout << std::hex << std::showbase << std::uppercase << u32 << std::endl;
42-
std::cout << std::hex << std::showbase << std::uppercase << u64 << std::endl;
28+
const std::uint8_t lo8 { UINT8_C(0x34) };
29+
const std::uint8_t hi8 { UINT8_C(0x12) };
30+
const std::uint16_t u16 { MAKE_WORD(lo8, hi8) };
31+
32+
const std::uint16_t lo16 { UINT16_C(0x5678) };
33+
const std::uint16_t hi16 { UINT16_C(0x1234) };
34+
const std::uint32_t u32 { MAKE_DWORD(lo16, hi16) };
35+
36+
const std::uint32_t lo32 { UINT32_C(0x9ABC'DEF0) };
37+
const std::uint32_t hi32 { UINT32_C(0x1234'5678) };
38+
const std::uint64_t u64 { MAKE_QWORD(lo32, hi32) };
39+
40+
std::stringstream strm { };
41+
42+
strm << std::hex << std::showbase << std::uppercase << u16 << '\n';
43+
strm << std::hex << std::showbase << std::uppercase << u32 << '\n';
44+
strm << std::hex << std::showbase << std::uppercase << u64 << '\n';
45+
46+
std::cout << strm.str() << std::endl;
4347
}
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019- 2020.
2+
// Copyright Christopher Kormanyos 2019- 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

8-
// chapter05_02-001_make_large.cpp
8+
// chapter05_02-002_make_large.cpp
99

1010
#include <cstdint>
1111
#include <iomanip>
1212
#include <iostream>
1313
#include <limits>
14+
#include <sstream>
1415

1516
template<typename ularge_type,
1617
typename ushort_type>
@@ -33,24 +34,27 @@ ularge_type make_large(const ushort_type& lo,
3334
return static_cast<ularge_type>(uh | lo);
3435
}
3536

36-
int main()
37+
auto main() -> int;
38+
39+
auto main() -> int
3740
{
38-
std::uint8_t lo8 = UINT8_C(0x34);
39-
std::uint8_t hi8 = UINT8_C(0x12);
40-
std::uint16_t u16
41-
= make_large<std::uint16_t>(lo8, hi8);
42-
43-
std::uint16_t lo16 = UINT16_C(0x5678);
44-
std::uint16_t hi16 = UINT16_C(0x1234);
45-
std::uint32_t u32
46-
= make_large<std::uint32_t>(lo16, hi16);
47-
48-
std::uint32_t lo32 = UINT32_C(0x9ABC'DEF0);
49-
std::uint32_t hi32 = UINT32_C(0x1234'5678);
50-
std::uint64_t u64
51-
= make_large<std::uint64_t>(lo32, hi32);
52-
53-
std::cout << std::hex << std::showbase << std::uppercase << u16 << std::endl;
54-
std::cout << std::hex << std::showbase << std::uppercase << u32 << std::endl;
55-
std::cout << std::hex << std::showbase << std::uppercase << u64 << std::endl;
41+
const std::uint8_t lo8 { UINT8_C(0x34) };
42+
const std::uint8_t hi8 { UINT8_C(0x12) };
43+
const std::uint16_t u16 { make_large<std::uint16_t>(lo8, hi8) };
44+
45+
const std::uint16_t lo16 { UINT16_C(0x5678) };
46+
const std::uint16_t hi16 { UINT16_C(0x1234) };
47+
const std::uint32_t u32 { make_large<std::uint32_t>(lo16, hi16) };
48+
49+
const std::uint32_t lo32 { UINT32_C(0x9ABC'DEF0) };
50+
const std::uint32_t hi32 { UINT32_C(0x1234'5678) };
51+
const std::uint64_t u64 { make_large<std::uint64_t>(lo32, hi32) };
52+
53+
std::stringstream strm { };
54+
55+
strm << std::hex << std::showbase << std::uppercase << u16 << '\n';
56+
strm << std::hex << std::showbase << std::uppercase << u32 << '\n';
57+
strm << std::hex << std::showbase << std::uppercase << u64 << '\n';
58+
59+
std::cout << strm.str() << std::endl;
5660
}
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2017 - 2018.
2+
// Copyright Christopher Kormanyos 2017 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -10,22 +10,23 @@
1010
#include <cstdint>
1111
#include <iomanip>
1212
#include <iostream>
13+
#include <sstream>
1314

1415
class communication
1516
{
1617
public:
17-
communication() { }
18+
communication() = default;
1819

19-
virtual ~communication() { }
20+
~communication() = default;
2021

21-
virtual bool send_byte(const std::uint8_t b) const
22+
auto send_byte(const std::uint8_t b) const -> bool
2223
{
24+
std::stringstream strm { };
25+
2326
// Simulate sending a byte on the PC.
24-
std::cout << "Sending: "
25-
<< std::hex
26-
<< std::showbase
27-
<< unsigned(b)
28-
<< std::endl;
27+
strm << "Sending: " << std::hex << std::showbase << unsigned(b);
28+
29+
std::cout << strm.str() << std::endl;
2930

3031
const std::ios::iostate cout_state = std::cout.rdstate();
3132

@@ -34,23 +35,26 @@ class communication
3435
return send_byte_is_ok;
3536
}
3637

37-
std::uint8_t recv_byte() const
38+
auto recv_byte() const noexcept -> std::uint8_t
3839
{
3940
return recv_buffer;
4041
}
4142

4243
private:
43-
std::uint8_t recv_buffer;
44+
std::uint8_t recv_buffer { };
4445
};
4546

46-
int main()
47+
auto main() -> int;
48+
49+
auto main() -> int
4750
{
48-
communication com;
51+
communication com { };
52+
53+
const bool send_byte_is_ok { com.send_byte(0x55U) };
54+
55+
std::stringstream strm { };
4956

50-
const bool send_byte_is_ok = com.send_byte(0x55U);
57+
strm << "Result of send_byte: " << std::boolalpha << send_byte_is_ok;
5158

52-
std::cout << "Result of send_byte: "
53-
<< std::boolalpha
54-
<< send_byte_is_ok
55-
<< std::endl;
59+
std::cout << strm.str() << std::endl;
5660
}

0 commit comments

Comments
 (0)