From 1d8ed2b59e449c4858901a4f2533a08658cbae4b Mon Sep 17 00:00:00 2001 From: jeanphilippeD Date: Mon, 24 Jul 2017 19:38:45 +0100 Subject: [PATCH] Fix CP.3 to allow local reasoning and compilation without error (#951) * Fix CP.3 to allow local reasoning and compilation without error (CP.3: Minimize explicit sharing of writable data) Use 'ES.28: Use lambdas for complex initialization, especially of const variables' to ensure we can see from the body of the function that the function called in async only require const parameter. This ensure that if a non local change to the function signature (making the const parameter non const) it will result in a compilation error. Added necessary construct and compiled with https://godbolt.org/g/tjGXbV * Update CppCoreGuidelines.md --- CppCoreGuidelines.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index f587cd6..e54b018 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -13319,23 +13319,20 @@ The less sharing you do, the less chance you have to wait on a lock (so performa Image altitude_map(const vector&); // ... - void process_readings(istream& socket1) + void process_readings(const Readings& surface_readings, istream& socket1) { - vector surface_readings; - socket1 >> surface_readings; - if (!socket1) throw Bad_input{}; - auto h1 = async([&] { if (!validate(surface_readings)) throw Invalid_data{}; }); auto h2 = async([&] { return temperature_gradiants(surface_readings); }); auto h3 = async([&] { return altitude_map(surface_readings); }); // ... - auto v1 = h1.get(); + h1.get(); auto v2 = h2.get(); auto v3 = h3.get(); // ... } Without those `const`s, we would have to review every asynchronously invoked function for potential data races on `surface_readings`. +Making `surface_readings` const allow reasoning using only the function body. ##### Note