What is the meaning of this piece of Standardese about shared_ptr's use_count()?

前端 未结 3 1476
伪装坚强ぢ
伪装坚强ぢ 2021-01-07 17:41

While trying to wrap my head around the problem shown in this question I found myself stuck on the following sentence from [util.smartptr.shared]/4:

[

3条回答
  •  鱼传尺愫
    2021-01-07 18:06

    I think when we add the previous sentence the intent becomes a little clearer:

    For purposes of determining the presence of a data race, member functions shall access and modify only the shared_ptr and weak_ptr objects themselves and not objects they refer to. Changes in use_count() do not reflect modifications that can introduce data races.

    So, the last sentence is just emphasizing the same point as the first sentence. For example, if I copy a shared_ptr, its use-count will be incremented to reflect the fact that the shared_ptr has been copied--therefore the result from use_count() will be changed--but this is not allowed to access (and, especially, not allowed to modify) the pointee object, so it can never introduce a data race in use of that pointee object.

提交回复
热议问题