From 23f8dfea8218adda9e86ea0a3890909685748e1c Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Fri, 16 Jun 2023 22:57:25 +0100 Subject: [PATCH 1/3] add missing headers Two headers needed for compilation on gcc and clang + #include // std::accumulate + #include // std::mem_fn (Apple Clang only needs ) --- listings/listing_8.3.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/listings/listing_8.3.cpp b/listings/listing_8.3.cpp index 86b8526..7e6a0b2 100644 --- a/listings/listing_8.3.cpp +++ b/listings/listing_8.3.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include template struct accumulate_block { From e0edfc8bcdeaf0ffd005eee9be11744ea4dcf6ac Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:02:57 +0100 Subject: [PATCH 2/3] need to wrap `std::packaged_task` constructor "Parentheses were disambiguated as a function declaration" --- listings/listing_8.3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/listing_8.3.cpp b/listings/listing_8.3.cpp index 7e6a0b2..ed251a9 100644 --- a/listings/listing_8.3.cpp +++ b/listings/listing_8.3.cpp @@ -42,7 +42,7 @@ T parallel_accumulate(Iterator first,Iterator last,T init) Iterator block_end=block_start; std::advance(block_end,block_size); std::packaged_task task( - accumulate_block()); + (accumulate_block())); futures[i]=task.get_future(); threads[i]=std::thread(std::move(task),block_start,block_end); block_start=block_end; From 4b5a1433357045ba3858fbece88947777749e607 Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:09:25 +0100 Subject: [PATCH 3/3] calling wrong constructor for `accumulate_block` - extra parentheses needed to call `accumulate_block::operator()` in place of default constructor "No matching constructor for initialization of 'accumulate_block, int>'" --- listings/listing_8.3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/listing_8.3.cpp b/listings/listing_8.3.cpp index ed251a9..e5af8d5 100644 --- a/listings/listing_8.3.cpp +++ b/listings/listing_8.3.cpp @@ -47,7 +47,7 @@ T parallel_accumulate(Iterator first,Iterator last,T init) threads[i]=std::thread(std::move(task),block_start,block_end); block_start=block_end; } - T last_result=accumulate_block(block_start,last); + T last_result=accumulate_block()(block_start,last); std::for_each(threads.begin(),threads.end(), std::mem_fn(&std::thread::join));