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
This commit is contained in:
jeanphilippeD 2017-07-24 19:38:45 +01:00 committed by Herb Sutter
parent 0de765443a
commit 1d8ed2b59e

View File

@ -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<Reading>&); Image altitude_map(const vector<Reading>&);
// ... // ...
void process_readings(istream& socket1) void process_readings(const Readings& surface_readings, istream& socket1)
{ {
vector<Reading> surface_readings;
socket1 >> surface_readings;
if (!socket1) throw Bad_input{};
auto h1 = async([&] { if (!validate(surface_readings)) throw Invalid_data{}; }); auto h1 = async([&] { if (!validate(surface_readings)) throw Invalid_data{}; });
auto h2 = async([&] { return temperature_gradiants(surface_readings); }); auto h2 = async([&] { return temperature_gradiants(surface_readings); });
auto h3 = async([&] { return altitude_map(surface_readings); }); auto h3 = async([&] { return altitude_map(surface_readings); });
// ... // ...
auto v1 = h1.get(); h1.get();
auto v2 = h2.get(); auto v2 = h2.get();
auto v3 = h3.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`. 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 ##### Note