Skip to content

Commit b007fc4

Browse files
committed
Modernize Chapter_03 snippets
1 parent e86d5f0 commit b007fc4

40 files changed

Lines changed: 181 additions & 183 deletions

File tree

Lines changed: 8 additions & 8 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)
@@ -11,21 +11,21 @@
1111

1212
void initialize() { std::cout << "initialization" << std::endl; }
1313

14-
void use_i(const int i) { std::cout << "i = " << i << std::endl; }
15-
void use_j(const int j) { std::cout << "j = " << j << std::endl; }
16-
void use_k(const int k) { std::cout << "k = " << k << std::endl; }
14+
auto use_i(const int i) -> void { std::cout << "i = " << i << std::endl; }
15+
auto use_j(const int j) -> void { std::cout << "j = " << j << std::endl; }
16+
auto use_k(const int k) -> void { std::cout << "k = " << k << std::endl; }
1717

18-
void do_something()
18+
auto do_something() -> void
1919
{
2020
// Initialize someting.
2121
initialize();
2222

2323
// Declare i when using it in use_i().
24-
const int i = 3;
24+
const int i { 3 };
2525
use_i(i);
2626

2727
// Declare j when using it in use_j().
28-
const int j = 7;
28+
const int j { 7 };
2929
use_j(j);
3030

3131
// Declare k in the scope of the for-loop.
@@ -35,7 +35,7 @@ void do_something()
3535
}
3636
}
3737

38-
int main()
38+
auto main() -> int
3939
{
4040
do_something();
4141
}

code_snippets/chapter03/chapter03_02-001_fixed_size_integer.cpp

Lines changed: 5 additions & 5 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)
@@ -12,13 +12,13 @@
1212
#include <iostream>
1313

1414
// This has *exactly* 16-bits signed.
15-
constexpr std::int16_t value16 = INT16_C(0x7FFF);
15+
constexpr std::int16_t value16 { INT16_C(0x7FFF) };
1616

1717
// This has *at least* 32-bits unsigned.
18-
constexpr std::uint_least32_t value32 =
19-
UINT32_C(4'294'967'295);
18+
constexpr std::uint_least32_t value32
19+
{ UINT32_C(4'294'967'295) };
2020

21-
int main()
21+
auto main() -> int
2222
{
2323
std::cout << std::hex << std::showbase << value16 << std::endl;
2424

code_snippets/chapter03/chapter03_02-002_prime_number.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2017 - 2023.
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)
@@ -18,9 +18,9 @@
1818
// Types like uint32_t and macros such as UINT32_C() are portable.
1919
// See also https://godbolt.org/z/3c38v3EY5
2020

21-
constexpr auto prime_664999 = static_cast<std::uint32_t>(UINT32_C(10'006'721));
21+
constexpr std::uint32_t prime_664999 { UINT32_C(10'006'721) };
2222

23-
int main()
23+
auto main() -> int
2424
{
2525
std::cout << prime_664999 << std::endl;
2626
}

code_snippets/chapter03/chapter03_02-002a_explore_prime_numbers.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2017 - 2023.
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)
@@ -22,7 +22,7 @@ namespace
2222
{
2323
template<typename unsigned_integral_type = std::uint32_t,
2424
const unsigned_integral_type maximum_value = static_cast<std::uint32_t>(UINT32_C(10006730))>
25-
void compute_primes_via_sieve(std::vector<unsigned_integral_type>& primes)
25+
auto compute_primes_via_sieve(std::vector<unsigned_integral_type>& primes) -> void
2626
{
2727
// Use a sieve algorithm to generate
2828
// a uint8 table of primes.
@@ -99,7 +99,7 @@ namespace
9999
}
100100
}
101101

102-
int main()
102+
auto main() -> int
103103
{
104104
using unsigned_integral_prime_type = std::uint32_t;
105105

code_snippets/chapter03/chapter03_03-001_bool_type.cpp

Lines changed: 7 additions & 7 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,15 +10,15 @@
1010
#include <iomanip>
1111
#include <iostream>
1212

13-
bool valid() { return true; }
14-
bool login() { return true; }
13+
auto valid() -> bool { return true; }
14+
auto login() -> bool { return true; }
1515

16-
void start_session() { }
16+
auto start_session() -> void { }
1717

18-
void do_something()
18+
auto do_something() -> void
1919
{
2020
// This Boolean test yields true or false.
21-
const bool session_is_ok = (valid() && login());
21+
const bool session_is_ok { valid() && login() };
2222

2323
// This tests if (session_is_ok == true).
2424
if(session_is_ok)
@@ -31,7 +31,7 @@ void do_something()
3131
}
3232
}
3333

34-
int main()
34+
auto main() -> int
3535
{
3636
do_something();
3737
}
Lines changed: 8 additions & 8 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)
@@ -14,34 +14,34 @@
1414
namespace mcal
1515
{
1616
// The mcal initialization.
17-
void init();
17+
auto init() -> void;
1818

1919
// The general purpose timer stuff in the mcal.
2020
namespace gpt
2121
{
22-
void init() { std::cout << "in gpt::init()" << std::endl; }
22+
auto init() void { std::cout << "in gpt::init()" << std::endl; }
2323

24-
std::uint32_t get_time_elapsed() { return UINT32_C(0); }
24+
auto get_time_elapsed() -> std::uint32_t { return UINT32_C(0); }
2525
}
2626

2727
// The ADC stuff in the mcal.
2828
namespace adc
2929
{
30-
void init() { std::cout << "in adc::init()" << std::endl; }
30+
auto init() -> void { std::cout << "in adc::init()" << std::endl; }
3131

32-
std::uint16_t read_value(const unsigned) { return UINT16_C(0); }
32+
auto read_value(const unsigned) -> std::uint16_t { return UINT16_C(0); }
3333
}
3434
}
3535

3636
// Initialize the mcal.
3737
// Note the clean organization with namespaces.
38-
void mcal::init()
38+
auto mcal::init() -> void
3939
{
4040
mcal::gpt::init();
4141
mcal::adc::init();
4242
}
4343

44-
int main()
44+
auto main() -> int
4545
{
4646
mcal::init();
4747
}

code_snippets/chapter03/chapter03_04-002_anonymous_namespace.cpp

Lines changed: 2 additions & 2 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)
@@ -15,7 +15,7 @@ namespace
1515
unsigned local_counter;
1616
}
1717

18-
int main()
18+
auto main() -> int
1919
{
2020
++local_counter;
2121

code_snippets/chapter03/chapter03_04-003_nested_namespace.cpp

Lines changed: 4 additions & 4 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)
@@ -12,15 +12,15 @@
1212
// A nested namespace definition.
1313
namespace X::Y::Z
1414
{
15-
int value;
15+
int value { };
1616
}
1717

18-
void do_something()
18+
auto do_something() -> void
1919
{
2020
X::Y::Z::value = 1;
2121
}
2222

23-
int main()
23+
auto main() -> int
2424
{
2525
do_something();
2626

code_snippets/chapter03/chapter03_05-001_basic_classes.cpp

Lines changed: 8 additions & 8 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)
@@ -14,15 +14,15 @@
1414
class point
1515
{
1616
public:
17-
std::uint8_t my_x;
18-
std::uint8_t my_y;
17+
std::uint8_t my_x { };
18+
std::uint8_t my_y { };
1919

2020
point(const std::uint8_t x = UINT8_C(0),
2121
const std::uint8_t y = UINT8_C(0)) : my_x(x),
2222
my_y(y)
2323
{ }
2424

25-
std::uint16_t squared_euclidean_distance() const
25+
auto squared_euclidean_distance() const -> std::uint16_t
2626
{
2727
// Squared Euclidean distance from the origin.
2828
const std::uint16_t x2(std::uint16_t(my_x) * my_x);
@@ -32,7 +32,7 @@ class point
3232
}
3333
};
3434

35-
point p1;
35+
point p1 { };
3636

3737
point p2
3838
{
@@ -41,12 +41,12 @@ point p2
4141
};
4242

4343
// The squared Euclidean distance d1 is 0.
44-
std::uint16_t d1 = p1.squared_euclidean_distance();
44+
std::uint16_t d1 { p1.squared_euclidean_distance() };
4545

4646
// The squared Euclidean distance d2 is 3,170.
47-
std::uint16_t d2 = p2.squared_euclidean_distance();
47+
std::uint16_t d2 { p2.squared_euclidean_distance() };
4848

49-
int main()
49+
auto main() -> int
5050
{
5151
std::cout << "d1 is " << d1 << std::endl;
5252
std::cout << "d2 is " << d2 << std::endl;

code_snippets/chapter03/chapter03_06-001_basic_templates_add.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2017 - 2023.
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)
@@ -12,22 +12,22 @@
1212
#include <string>
1313

1414
template<typename T>
15-
T add(const T& a, const T& b)
15+
constexpr auto add(const T& a, const T& b) -> T
1616
{
1717
return a + b;
1818
}
1919

20-
const int n = add(1, 2);
20+
constexpr int n { add(1, 2) };
2121
// n is 3.
2222

23-
const float f = add(1.2F, 3.4F);
23+
constexpr float f { add(1.2F, 3.4F) };
2424
// f is 4.6.
2525

26-
const std::string str = add(std::string("abc"),
27-
std::string("xyz"));
26+
const std::string str
27+
{ add(std::string("abc"), std::string("xyz")) };
2828
// str is "abcxyz".
2929

30-
int main()
30+
auto main() -> int
3131
{
3232
std::cout << "int addition result is " << n << std::endl;
3333
std::cout << "float addition result is " << f << std::endl;

0 commit comments

Comments
 (0)