mirror of
https://gitlab.com/fbb-git/cppannotations
synced 2024-11-16 07:48:44 +01:00
fixed code flaw
This commit is contained in:
parent
9fa1e8009e
commit
4da70393bb
4 changed files with 52 additions and 4 deletions
|
@ -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
|
||||
|
||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
|
|
@ -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)
|
||||
|
|
40
annotations/yo/first/examples/bindings.cc
Normal file
40
annotations/yo/first/examples/bindings.cc
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <cctype>
|
||||
#include <iostream>
|
||||
|
||||
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';
|
||||
}
|
|
@ -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
|
||||
{}
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue