diff --git a/annotations/yo/WIP b/annotations/yo/WIP index 45ff5fb0..f67b164b 100644 --- a/annotations/yo/WIP +++ b/annotations/yo/WIP @@ -15,7 +15,7 @@ Relaxing the range-for loop customization point finding rules P0962R1 (unclear in P0962R1 what doesn't work: the example that supposedly doesn't compile compiles and run on at least g++-6) - Allow structured bindings to accessible members P0969R0 8 +Allow structured bindings to accessible members P0969R0 8 Relaxing the structured bindings customization point finding rules P0961R1 8 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/annotations/yo/first/binding.yo b/annotations/yo/first/binding.yo index 93dfccb0..a567e3fd 100644 --- a/annotations/yo/first/binding.yo +++ b/annotations/yo/first/binding.yo @@ -118,3 +118,11 @@ tuples. Here is an example illustrating this: Year 10: amount = 1343.92 */ ) + + A somewhat different application of structured binding declarations is +encountered when accessing data members of structs (or classes, cf. chapter +ref(CLASSES)). If the data members of a tt(struct) or tt(class) object are +em(all) visible then structured bindings may be used to directly refer to the +object's data. The structured binding declaration's variables refer to the +object's data members from the first to the last. Here is an example: + verbinsert(-as4 examples/bindings.cc) diff --git a/annotations/yo/first/examples/bindings.cc b/annotations/yo/first/examples/bindings.cc new file mode 100644 index 00000000..06298585 --- /dev/null +++ b/annotations/yo/first/examples/bindings.cc @@ -0,0 +1,40 @@ +#include +#include + +using namespace std; + +struct Fails +{ + int d_int = 0; + double d_double = 0; + int (*d_is)(int) = isalnum; + + private: + int d_private; +}; + +struct Data +{ + int d_int = 0; + double d_double = 0; + int (*d_is)(int) = isalnum; +}; + +int main() +{ + Fails fails; + + // auto &[i, d, fun, priv] = fails; // won't compile: d_private + // is inaccessible + + // auto &[i, d, fun] = fails; // won't compile: not all of Fails's + // members are used + + Data data; + + auto &[i, d, fun] = data; // OK + + // d_int modified through ++i + cout << ++i << ' ' << data.d_int << ' ' << + fun('a') << ' ' << fun(',') << '\n'; +} diff --git a/annotations/yo/inheritance/constructor.yo b/annotations/yo/inheritance/constructor.yo index f60a3e46..d25b43d4 100644 --- a/annotations/yo/inheritance/constructor.yo +++ b/annotations/yo/inheritance/constructor.yo @@ -52,12 +52,12 @@ constructor may therefore be improved: Derived class constructors always by default call their base class's default constructor. This is of course not correct for a derived class's i(copy constructor). Assuming that the class tt(Land) must be provided with a -copy constructor it may use the tt(Land const &other) to represent the other's -base class: +copy constructor its tt(Land const &other) parameter also represents the other +object's base class: verb( Land::Land(Land const &other) // assume a copy constructor is needed : Vehicle(other), // copy-construct the base class part. - d_speed(other.speed) // copy-construct Land's data members + d_speed(other.d_speed) // copy-construct Land's data members {} )