From Jason Turner

[util.smartptr.enab]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpa19ci4hm/{from.md → to.md} +20 -38
tmp/tmpa19ci4hm/{from.md → to.md} RENAMED
@@ -1,90 +1,72 @@
1
  #### Class template `enable_shared_from_this` <a id="util.smartptr.enab">[[util.smartptr.enab]]</a>
2
 
3
  A class `T` can inherit from `enable_shared_from_this<T>` to inherit the
4
- `shared_from_this` member functions that obtain a *shared_ptr* instance
5
  pointing to `*this`.
6
 
 
 
7
  ``` cpp
8
- struct X: public enable_shared_from_this<X> {
9
- };
10
 
11
  int main() {
12
  shared_ptr<X> p(new X);
13
  shared_ptr<X> q = p->shared_from_this();
14
  assert(p == q);
15
- assert(!(p < q ) && !(q < p)); // p and q share ownership
16
  }
17
  ```
18
 
 
 
19
  ``` cpp
20
  namespace std {
21
  template<class T> class enable_shared_from_this {
22
  protected:
23
  constexpr enable_shared_from_this() noexcept;
24
- enable_shared_from_this(enable_shared_from_this const&) noexcept;
25
- enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
26
  ~enable_shared_from_this();
27
  public:
28
  shared_ptr<T> shared_from_this();
29
  shared_ptr<T const> shared_from_this() const;
 
 
 
 
30
  };
31
- } // namespace std
32
  ```
33
 
34
  The template parameter `T` of `enable_shared_from_this` may be an
35
  incomplete type.
36
 
37
  ``` cpp
38
  constexpr enable_shared_from_this() noexcept;
39
  enable_shared_from_this(const enable_shared_from_this<T>&) noexcept;
40
  ```
41
 
42
- *Effects:* Constructs an `enable_shared_from_this<T>` object.
43
 
44
  ``` cpp
45
  enable_shared_from_this<T>& operator=(const enable_shared_from_this<T>&) noexcept;
46
  ```
47
 
48
  *Returns:* `*this`.
49
 
50
- ``` cpp
51
- ~enable_shared_from_this();
52
- ```
53
-
54
- *Effects:* Destroys `*this`.
55
 
56
  ``` cpp
57
  shared_ptr<T> shared_from_this();
58
  shared_ptr<T const> shared_from_this() const;
59
  ```
60
 
61
- *Requires:* `enable_shared_from_this<T>` shall be an accessible base
62
- class of `T`. `*this` shall be a subobject of an object `t` of type `T`.
63
- There shall be at least one `shared_ptr` instance `p` that *owns* `&t`.
64
-
65
- *Returns:* A `shared_ptr<T>` object `r` that *shares ownership with*
66
- `p`.
67
-
68
- *Postconditions:* `r.get() == this`.
69
-
70
- A possible implementation is shown below:
71
 
72
  ``` cpp
73
- template<class T> class enable_shared_from_this {
74
- private:
75
- weak_ptr<T> __weak_this;
76
- protected:
77
- constexpr enable_shared_from_this() : __weak_this() { }
78
- enable_shared_from_this(enable_shared_from_this const &) { }
79
- enable_shared_from_this& operator=(enable_shared_from_this const &) { return *this; }
80
- ~enable_shared_from_this() { }
81
- public:
82
- shared_ptr<T> shared_from_this() { return shared_ptr<T>(__weak_this); }
83
- shared_ptr<T const> shared_from_this() const { return shared_ptr<T const>(__weak_this); }
84
- };
85
  ```
86
 
87
- The `shared_ptr` constructors that create unique pointers can detect the
88
- presence of an `enable_shared_from_this` base and assign the newly
89
- created `shared_ptr` to its `__weak_this` member.
90
 
 
1
  #### Class template `enable_shared_from_this` <a id="util.smartptr.enab">[[util.smartptr.enab]]</a>
2
 
3
  A class `T` can inherit from `enable_shared_from_this<T>` to inherit the
4
+ `shared_from_this` member functions that obtain a `shared_ptr` instance
5
  pointing to `*this`.
6
 
7
+ [*Example 1*:
8
+
9
  ``` cpp
10
+ struct X: public enable_shared_from_this<X> { };
 
11
 
12
  int main() {
13
  shared_ptr<X> p(new X);
14
  shared_ptr<X> q = p->shared_from_this();
15
  assert(p == q);
16
+ assert(!p.owner_before(q) && !q.owner_before(p)); // p and q share ownership
17
  }
18
  ```
19
 
20
+ — *end example*]
21
+
22
  ``` cpp
23
  namespace std {
24
  template<class T> class enable_shared_from_this {
25
  protected:
26
  constexpr enable_shared_from_this() noexcept;
27
+ enable_shared_from_this(const enable_shared_from_this&) noexcept;
28
+ enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept;
29
  ~enable_shared_from_this();
30
  public:
31
  shared_ptr<T> shared_from_this();
32
  shared_ptr<T const> shared_from_this() const;
33
+ weak_ptr<T> weak_from_this() noexcept;
34
+ weak_ptr<T const> weak_from_this() const noexcept;
35
+ private:
36
+ mutable weak_ptr<T> weak_this; // exposition only
37
  };
38
+ }
39
  ```
40
 
41
  The template parameter `T` of `enable_shared_from_this` may be an
42
  incomplete type.
43
 
44
  ``` cpp
45
  constexpr enable_shared_from_this() noexcept;
46
  enable_shared_from_this(const enable_shared_from_this<T>&) noexcept;
47
  ```
48
 
49
+ *Effects:* Value-initializes `weak_this`.
50
 
51
  ``` cpp
52
  enable_shared_from_this<T>& operator=(const enable_shared_from_this<T>&) noexcept;
53
  ```
54
 
55
  *Returns:* `*this`.
56
 
57
+ [*Note 1*: `weak_this` is not changed. — *end note*]
 
 
 
 
58
 
59
  ``` cpp
60
  shared_ptr<T> shared_from_this();
61
  shared_ptr<T const> shared_from_this() const;
62
  ```
63
 
64
+ *Returns:* `shared_ptr<T>(weak_this)`.
 
 
 
 
 
 
 
 
 
65
 
66
  ``` cpp
67
+ weak_ptr<T> weak_from_this() noexcept;
68
+ weak_ptr<T const> weak_from_this() const noexcept;
 
 
 
 
 
 
 
 
 
 
69
  ```
70
 
71
+ *Returns:* `weak_this`.
 
 
72