From 5378c6c3a29550cd3ccf4514e0f2793a507e4673 Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Sat, 8 Jul 2023 15:14:12 +0100 Subject: [PATCH 1/3] opt for `std::make_unique<>` over `operator new` - see Herb Sutter's talk - https://herbsutter.com/gotw/_102/ --- listings/listing_9.2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/listing_9.2.cpp b/listings/listing_9.2.cpp index bb32073..b0830b9 100644 --- a/listings/listing_9.2.cpp +++ b/listings/listing_9.2.cpp @@ -22,7 +22,7 @@ class function_wrapper public: template function_wrapper(F&& f): - impl(new impl_type(std::move(f))) + impl(std::make_unique>(std::move(f))) {} void call() { impl->call(); } From a12f8ac4c83abdc1472b42e86c18e545026f4ec6 Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Sat, 8 Jul 2023 15:37:53 +0100 Subject: [PATCH 2/3] `std::result_of` -> `std::invoke_result_t` - `std::result_of` was deprecated as of C++17, and removed in C++20 - opt for `std::invoke_result_t` in its place for all instances - also improves brevity --- listings/listing_9.2.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/listings/listing_9.2.cpp b/listings/listing_9.2.cpp index b0830b9..79b500b 100644 --- a/listings/listing_9.2.cpp +++ b/listings/listing_9.2.cpp @@ -48,10 +48,10 @@ class thread_pool std::deque work_queue; template - std::future::type> + std::future> submit(FunctionType f) { - typedef typename std::result_of::type result_type; + typedef std::invoke_result_t result_type; std::packaged_task task(std::move(f)); std::future res(task.get_future()); From 797213f6bad2c3acc80dafe58d58aaea3088c5f8 Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Sat, 8 Jul 2023 15:47:37 +0100 Subject: [PATCH 3/3] use of `noexcept` with move(-assignment) operations - move and move-assignment operations should not throw --- listings/listing_9.2.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/listings/listing_9.2.cpp b/listings/listing_9.2.cpp index 79b500b..801e672 100644 --- a/listings/listing_9.2.cpp +++ b/listings/listing_9.2.cpp @@ -21,17 +21,17 @@ class function_wrapper }; public: template - function_wrapper(F&& f): + function_wrapper(F&& f) noexcept: impl(std::make_unique>(std::move(f))) {} void call() { impl->call(); } - function_wrapper(function_wrapper&& other): + function_wrapper(function_wrapper&& other) noexcept: impl(std::move(other.impl)) {} - function_wrapper& operator=(function_wrapper&& other) + function_wrapper& operator=(function_wrapper&& other) noexcept { impl=std::move(other.impl); return *this;