diff --git a/scientific_cpp.pptx b/scientific_cpp.pptx index 6b6d341..eba8d46 100644 Binary files a/scientific_cpp.pptx and b/scientific_cpp.pptx differ diff --git a/source-code/C++17/README.md b/source-code/C++17/README.md deleted file mode 100644 index 65e0e0e..0000000 --- a/source-code/C++17/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# C++17 -A few illustrations of features added in the C++17 standard. - -## What is it? -1. `if_init.cpp`: variable defition in `if`-statment. -1. `optional.cpp`: illustrations the use of `std::optional`. -1. `structured_bindings.cpp`: illustrates structured binding of a - `struct` to individual variables. -1. `variant_visit.cpp`: illustration of how to use `std::variant` and `std::visit`. -1. `Makefile`: make file to build the applications. diff --git a/source-code/ErrorHandling/Exceptions/CMakeLists.txt b/source-code/ErrorHandling/Exceptions/CMakeLists.txt new file mode 100644 index 0000000..5e1d34d --- /dev/null +++ b/source-code/ErrorHandling/Exceptions/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.20) + +project(exceptions LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +add_executable(fac.exe fac.cpp) diff --git a/source-code/ErrorHandling/Exceptions/README.md b/source-code/ErrorHandling/Exceptions/README.md new file mode 100644 index 0000000..c73d3b7 --- /dev/null +++ b/source-code/ErrorHandling/Exceptions/README.md @@ -0,0 +1,10 @@ +# Expected + +Illustrates how to use exceptoin to do error handling. + +## What is it? + +1. `fac.cpp`: C++ application that reads an argument from the command line, + validates the value, computes the factorial and displays the output or + the error message. +1. `CMakeLists.txt`: CMake file to build the applications. diff --git a/source-code/Modularity/fac.cpp b/source-code/ErrorHandling/Exceptions/fac.cpp similarity index 100% rename from source-code/Modularity/fac.cpp rename to source-code/ErrorHandling/Exceptions/fac.cpp diff --git a/source-code/ErrorHandling/Expected/CMakeLists.txt b/source-code/ErrorHandling/Expected/CMakeLists.txt new file mode 100644 index 0000000..8771628 --- /dev/null +++ b/source-code/ErrorHandling/Expected/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.20) + +project(expected LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +add_executable(fac.exe fac.cpp) diff --git a/source-code/ErrorHandling/Expected/README.md b/source-code/ErrorHandling/Expected/README.md new file mode 100644 index 0000000..2d3b996 --- /dev/null +++ b/source-code/ErrorHandling/Expected/README.md @@ -0,0 +1,11 @@ +# Expected + +Illustrates how to use `std::expected` for error handling, showcasing the +monadic interface (slightly over the top). + +## What is it? + +1. `fac.cpp`: C++ application that reads an argument from the command line, + validates the value, computes the factorial and displays the output or + the error message. +1. `CMakeLists.txt`: CMake file to build the applications. diff --git a/source-code/ErrorHandling/Expected/fac.cpp b/source-code/ErrorHandling/Expected/fac.cpp new file mode 100644 index 0000000..ff29bce --- /dev/null +++ b/source-code/ErrorHandling/Expected/fac.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +using ErrorResult = std::string; +using ParseResult = std::string; + +std::expected parse_arguments(int argc, char* argv[]) { + std::string app_name {argv[0]}; + if (argc != 2) { + return std::unexpected(app_name + " takes 1 argument"); + } + return ErrorResult(argv[1]); +} + +using ValidateResult = int; + +std::expected validate_arguments(const ParseResult& arg) { + try { + return ValidateResult(std::stoi(arg)); + } catch (const std::invalid_argument& e) { + return std::unexpected("can not convert '" + arg + "' to an integer"); + } catch (const std::out_of_range& e) { + return std::unexpected(arg + " is too large to convert to an integer"); + } +} + +using FunctionResult = int; + +std::expected fac(ValidateResult n) { + if (n < 0) { + return std::unexpected("fac argument " + std::to_string(n) + ", must be positive"); + } else { + const int max_int {std::numeric_limits::max()}; + int result = 1; + for (int i = 2; i <= n; i++) { + if (result < max_int/i) { + result *= i; + } else { + return std::unexpected("fac argument " + std::to_string(n) + " causes overflow"); + } + } + return FunctionResult(result); + } +} + +std::expected show_result(FunctionResult result) { + std::cout << result << std::endl; + return result; +} + +std::expected show_error(const ErrorResult& error) { + std::cerr << "# error: " << error << std::endl; + return std::unexpected(error); +} + +int main(int argc, char *argv[]) { + auto result = parse_arguments(argc, argv) + .and_then(validate_arguments) + .and_then(fac) + .and_then(show_result) + .or_else(show_error) ; + if (result) { + return 0; + } else { + return 1; + } + return 0; +} diff --git a/source-code/Modularity/Makefile b/source-code/ErrorHandling/MemoryLeak/Makefile similarity index 82% rename from source-code/Modularity/Makefile rename to source-code/ErrorHandling/MemoryLeak/Makefile index 8deca18..c33fdea 100644 --- a/source-code/Modularity/Makefile +++ b/source-code/ErrorHandling/MemoryLeak/Makefile @@ -2,7 +2,7 @@ CXX = g++ CXXFLAGS = -std=c++14 -g -O2 -Wall -Wextra LDLIBS = -lm -all: fac.exe memory_leak.exe memory_leak_fixed.exe +all: memory_leak.exe memory_leak_fixed.exe %.exe: %.o $(CXX) $(CXXFLAGS) -o $@ $< $(LDLIBS) diff --git a/source-code/ErrorHandling/MemoryLeak/README.md b/source-code/ErrorHandling/MemoryLeak/README.md new file mode 100644 index 0000000..87087d5 --- /dev/null +++ b/source-code/ErrorHandling/MemoryLeak/README.md @@ -0,0 +1,15 @@ +# Memory leak + +Code illustrating a memory leak created by inappropriate exception +handling. + +# What is it? +1. `memory_leak.cpp`: illustration of a memory leak caused by bad exception + handling. +1. `memory_leak_fixed.cpp`: illustrates using RAII to ensure correct resource + management. +1. `Makefile`: make file for these examples. + +## How to use it? +Run `memory_leak.exe` and `memory_leak_fixed.exe` through valgrind with -1 and 5 +as arguments and note the difference. diff --git a/source-code/Modularity/memory_leak.cpp b/source-code/ErrorHandling/MemoryLeak/memory_leak.cpp similarity index 100% rename from source-code/Modularity/memory_leak.cpp rename to source-code/ErrorHandling/MemoryLeak/memory_leak.cpp diff --git a/source-code/Modularity/memory_leak_fixed.cpp b/source-code/ErrorHandling/MemoryLeak/memory_leak_fixed.cpp similarity index 100% rename from source-code/Modularity/memory_leak_fixed.cpp rename to source-code/ErrorHandling/MemoryLeak/memory_leak_fixed.cpp diff --git a/source-code/ErrorHandling/README.md b/source-code/ErrorHandling/README.md new file mode 100644 index 0000000..d21219d --- /dev/null +++ b/source-code/ErrorHandling/README.md @@ -0,0 +1,10 @@ +# Error handling + +Code illustrations for chapter 3, Modularity in Stroustrup's +"A tour of C++" on error handling using exceptions. + +# What is it? +1. `Exceptions`: illustration of error handling with exceptions. +1. `MemoryLeak`: illustrates how inappropriately handled exceptions can + lead to memory leaks, and how to fix that. +1. `Expected`: illustrates error handling using `std::expected`. diff --git a/source-code/GeneralUtilities/CMakeLists.txt b/source-code/GeneralUtilities/CMakeLists.txt new file mode 100644 index 0000000..e1ed872 --- /dev/null +++ b/source-code/GeneralUtilities/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.20) + +project(general_utilities LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +add_executable(optional.exe optional.cpp) +add_executable(variant_visit.exe variant_visit.cpp) diff --git a/source-code/GeneralUtilities/README.md b/source-code/GeneralUtilities/README.md new file mode 100644 index 0000000..3f25986 --- /dev/null +++ b/source-code/GeneralUtilities/README.md @@ -0,0 +1,7 @@ +# General utilities +A few illustrations of features in the general utilities STL. + +## What is it? +1. `optional.cpp`: illustrations the use of `std::optional`. +1. `variant_visit.cpp`: illustration of how to use `std::variant` and `std::visit`. +1. `CMakeLists.txt`: CMake file to build the applications. diff --git a/source-code/C++17/optional.cpp b/source-code/GeneralUtilities/optional.cpp similarity index 100% rename from source-code/C++17/optional.cpp rename to source-code/GeneralUtilities/optional.cpp diff --git a/source-code/C++17/variant_visit.cpp b/source-code/GeneralUtilities/variant_visit.cpp similarity index 100% rename from source-code/C++17/variant_visit.cpp rename to source-code/GeneralUtilities/variant_visit.cpp diff --git a/source-code/C++17/.gitignore b/source-code/GrammarFeatures/.gitignore similarity index 100% rename from source-code/C++17/.gitignore rename to source-code/GrammarFeatures/.gitignore diff --git a/source-code/C++17/Makefile b/source-code/GrammarFeatures/Makefile similarity index 76% rename from source-code/C++17/Makefile rename to source-code/GrammarFeatures/Makefile index d2c3c86..9fb93cc 100644 --- a/source-code/C++17/Makefile +++ b/source-code/GrammarFeatures/Makefile @@ -1,7 +1,7 @@ CXX = g++ CXXFLAGS = -std=c++17 -O2 -g -Wall -Wextra -Wpedantic -all: if_init.exe optional.exe structured_bindings.exe variant_visit.exe +all: if_init.exe structured_bindings.exe %.exe: %.o $(CXX) $(CXXFLAGS) -o $@ $^ diff --git a/source-code/GrammarFeatures/README.md b/source-code/GrammarFeatures/README.md new file mode 100644 index 0000000..4a082f9 --- /dev/null +++ b/source-code/GrammarFeatures/README.md @@ -0,0 +1,8 @@ +# Grammar features +A few illustrations of grammar features added in recent C++ standards. + +## What is it? +1. `if_init.cpp`: variable defition in `if`-statment (C++17). +1. `structured_bindings.cpp`: illustrates structured binding of a + `struct` to individual variables (C++17). +1. `Makefile`: make file to build the applications. diff --git a/source-code/C++17/if_init.cpp b/source-code/GrammarFeatures/if_init.cpp similarity index 100% rename from source-code/C++17/if_init.cpp rename to source-code/GrammarFeatures/if_init.cpp diff --git a/source-code/C++17/structured_bindings.cpp b/source-code/GrammarFeatures/structured_bindings.cpp similarity index 100% rename from source-code/C++17/structured_bindings.cpp rename to source-code/GrammarFeatures/structured_bindings.cpp diff --git a/source-code/Modularity/README.md b/source-code/Modularity/README.md index ee5a44f..4471c5b 100644 --- a/source-code/Modularity/README.md +++ b/source-code/Modularity/README.md @@ -7,13 +7,3 @@ Code illustrations for chapter 3, Modularity in Stroustrup's 1. `Particles`: example of separate compilation. 1. `ParticlesCMake`: example of build using CMake. 1. `Stats`: example of separate compilation. -1. `fac.cpp`: illustrates exception handling. -1. `memory_leak.cpp`: illustration of a memory leak caused by bad exception - handling. -1. `memory_leak_fixed.cpp`: illustrates using RAII to ensure correct resource - management. -1. `Makefile`: make file for these examples. - -## How to use it? -Run `memory_leak.exe` and `memory_leak_fixed.exe` through valgrind with -1 and 5 -as arguments and note the difference. diff --git a/source-code/README.md b/source-code/README.md index 73df7af..a8d9f2a 100644 --- a/source-code/README.md +++ b/source-code/README.md @@ -33,7 +33,8 @@ programming concepts and idiosyncracies particular to C++. 1. `Boost`: some illustrations of using the Boost C++ library, mostly in the context of numerical programming. 1. `UsingCLibraries`: illustration of how to use a C library from C++ code. -1. `C++17`: illustrations of features added in the C++17 standard +1. `GrammarFeatures`: illustrations of recent C++ grammar features added in + recent C++ standarsd specification. 1. `Ranges`: ranges are an interesting concept for working more conveniently with STL containers and composing algorithms. @@ -47,3 +48,5 @@ programming concepts and idiosyncracies particular to C++. 1. `ParallelExecution`: illustration of parallelizing algorithms using execution policies and TBB. 1. `Cling`: Xeus-cling kernel in Jupyter Lab for interactive C++ development. +1. `GeneralUtilities`: examples of using STL general utilities. +1. `ErrorHandling`: examples of error handling. diff --git a/source-code/Templates/Concepts/CMakeLists.txt b/source-code/Templates/Concepts/CMakeLists.txt new file mode 100644 index 0000000..68ce12c --- /dev/null +++ b/source-code/Templates/Concepts/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.20) +project(grids LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 23) +set(CMaKE_CXX_STANDARD_REQUIRED YES) +set(CMAKE_CXX_EXTENSIONS NO) + +add_compile_options(-Wall -Wextra -Wpedantic) + +add_executable(iterable.exe main.cpp) diff --git a/source-code/Templates/Concepts/README.md b/source-code/Templates/Concepts/README.md index 073c531..a470abf 100644 --- a/source-code/Templates/Concepts/README.md +++ b/source-code/Templates/Concepts/README.md @@ -10,3 +10,4 @@ types. 1. `iterable.h`: defintion of the iterable concept. 1. `main.cpp`: application that will fail to compile because the template constraints are not satisfied. +1. `CMakeLists.txt`: CMake file to build the applications. diff --git a/source-code/Templates/Concepts/main.cpp b/source-code/Templates/Concepts/main.cpp index ebad1b0..4962eff 100644 --- a/source-code/Templates/Concepts/main.cpp +++ b/source-code/Templates/Concepts/main.cpp @@ -11,11 +11,24 @@ requires Iterable void print(T t) { std::cout << std::endl; } +template +void print2(T t) { + for (auto i : t) { + std::cout << i << " "; + } + std::cout << std::endl; +} + int main() { static_assert(Iterable>, "vector is iterable"); print(std::vector{1, 2, 3, 4, 5}); + print2(std::vector{1, 2, 3, 4, 5}); static_assert(Iterable>); - static_assert(Iterable); - print({1, 2, 3, 4, 5}); + float data[] = {1, 2, 3, 4, 5}; + std::span s{data}; + print(s); + print2(s); + // static_assert(Iterable); // error: float[] is not an iterable + // print({1, 2, 3, 4, 5}); // float[] is not an iterable return 0; }