C++ / a working model

BOOK / SOURCE & READING RECORD

C++ Primer

Stanley B. Lippman; Josée Lajoie; Barbara E. Moo

5th edition,2012年8月首印,版权2013;公开研究副本为1399页网页捕获PDF

READING EVIDENCE / Partial text read

C++ Primer

The extractable body text, code, exercises, terminology, and Appendix A plus the index of chapters 1–19 have been read in consecutive ranges; this is not limited to sample chapters. The image tables of Appendix A.1 have been additionally read from PDF pages 1057–1063. The research copy still contains tables, figures, and code that exist only as images and have not been restored one by one; some code transcriptions are truncated. Therefore no claim is made that every figure and piece of text in the original edition has been read in full. Exercises were not executed.

Edition, actual reading range, and original sources →

Sources

The level of copying determines object relationships

§12.1.1 shared_ptr and StrBlob; §12.1.6 weak_ptr; §13.1.4 Rule of Three/Five; §13.2 Copy Control and Resource Management

After reading about dynamic memory and then looking at copy control, one discovers that the two topics cannot be learned separately. Callers who copy a vector usually expect an independent value; copying a shared_ptr only adds another owner of the same object. The sharing behavior of StrBlob is well suited to making this difference visible. Default memberwise copying is not always wrong; the key is whether the member types already express the desired responsibilities. If storing a container directly meets the need, adding a handwritten destructor may instead interrupt generation of the default move operations. A modern synthesis therefore first writes down independent, shared, or exclusive semantics, then chooses the member representation, and only then discusses optimization. weak_ptr is an observing relationship that acknowledges the object may disappear; lock is the action that obtains a lifetime guarantee for the duration of use, not a count check that can replace all synchronization.

Related fundamentals

Deep copy, shallow copy, and the Rule of Zero / Fiveshared_ptr: Control Blocks and Thread-Safety Boundariesweak_ptr: Breaking Cycles and Safely Taking Temporary Ownership

Original practice →