Conversation
|
|
||
| ##### Example; bad | ||
|
|
||
| This can also come up with concurrency. Ensure that an async operation that access a value is joined before the value it accesses is destroyed. |
There was a problem hiding this comment.
| This can also come up with concurrency. Ensure that an async operation that access a value is joined before the value it accesses is destroyed. | |
| This can also come up with concurrency. Ensure that an async operation that accesses a value is joined before the value it accesses is destroyed. |
| class X { | ||
| struct B { | ||
| string* p; | ||
| explicit B(string& a) : p(&a) {} |
There was a problem hiding this comment.
| explicit B(string& a) : p(&a) {} | |
| explicit B(string& a) : p{&a} {} |
| string a = "some heap allocated string value"; // constructed after b; destroyed before b | ||
|
|
||
| public: | ||
| X() : b(a) {} // uses a before it is constructed -> use-before-alloc UB |
There was a problem hiding this comment.
| X() : b(a) {} // uses a before it is constructed -> use-before-alloc UB | |
| X() : b{a} {} // uses a before it is constructed -> use-before-alloc UB |
| class X { | ||
| struct B { | ||
| string* p; | ||
| explicit B(string& a) : p(&a) {} |
There was a problem hiding this comment.
| explicit B(string& a) : p(&a) {} | |
| explicit B(string& a) : p{&a} {} |
| B b; // constructed second | ||
|
|
||
| public: | ||
| X() : b(a) {} // ok |
There was a problem hiding this comment.
| X() : b(a) {} // ok | |
| X() : b{a} {} // ok |
| class X { | ||
| public: | ||
| X() | ||
| : a(std::make_unique<int>(12)) |
There was a problem hiding this comment.
| : a(std::make_unique<int>(12)) | |
| : a{std::make_unique<int>(12)} |
|
|
||
| ##### Example; good | ||
|
|
||
| This can also come up with concurrency. Ensure that an async operation that access a value is joined before the value it accesses is destroyed. |
There was a problem hiding this comment.
| This can also come up with concurrency. Ensure that an async operation that access a value is joined before the value it accesses is destroyed. | |
| This can also come up with concurrency. Ensure that an async operation that accesses a value is joined before the value it accesses is destroyed. |
| class X { | ||
| public: | ||
| X() | ||
| : a(std::make_unique<int>(12)) |
There was a problem hiding this comment.
| : a(std::make_unique<int>(12)) | |
| : a{std::make_unique<int>(12)} |
| Flag a data member that is `const`, `&`, or `&&` in a type that has any copy or move operation. | ||
|
|
||
|
|
||
| ### <a name="rc-lifetime"></a>C.13: If one data member uses another, declare it before the other |
There was a problem hiding this comment.
My reading of the title is (italic text is mine):
If one data member
Buses another data memberA, declare it (B) before the other (A)
This is in contrast to what the Discussion section says:
If data member
Buses another data memberA, thenAmust be declared beforeBso thatAoutlivesB
I am not a native English speaker. Is my reading of the title wrong?
There was a problem hiding this comment.
I agree with your interpretation of the title and that they contradict.
There was a problem hiding this comment.
Maybe title could be 'If one data member uses another, declare it after the used member' ?
There was a problem hiding this comment.
I think maybe it could rephrase to 'Declare a data member before members that depend on it' — it should convey roughly the same meaning?
|
|
||
| // Bad: b uses a, but a is declared after b. | ||
| // Construction order is b then a; destruction order is a then b. | ||
| // So b touches a outside a's lifetime. |
There was a problem hiding this comment.
looks like the build failed because a's isn't in the dictionary
There was a problem hiding this comment.
I think it could also rephrase this line to "So b touches a outside the lifetime of a.", which conveys the same meaning.
I think this phrasing might also express the intent more clearly, I'm not entirely sure
Closes #2316