tmp/tmp420dfpo4/{from.md → to.md}
RENAMED
|
@@ -1,20 +1,23 @@
|
|
| 1 |
-
###
|
| 2 |
|
| 3 |
The library provides templated helper functions to simplify applying
|
| 4 |
move semantics to an lvalue and to simplify the implementation of
|
| 5 |
-
forwarding functions.
|
|
|
|
| 6 |
|
| 7 |
``` cpp
|
| 8 |
template <class T> constexpr T&& forward(remove_reference_t<T>& t) noexcept;
|
| 9 |
template <class T> constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
|
| 10 |
```
|
| 11 |
|
| 12 |
*Returns:* `static_cast<T&&>(t)`.
|
| 13 |
|
| 14 |
-
If the second form is instantiated with an lvalue reference
|
| 15 |
-
program is ill-formed.
|
|
|
|
|
|
|
| 16 |
|
| 17 |
``` cpp
|
| 18 |
template <class T, class A1, class A2>
|
| 19 |
shared_ptr<T> factory(A1&& a1, A2&& a2) {
|
| 20 |
return shared_ptr<T>(new T(std::forward<A1>(a1), std::forward<A2>(a2)));
|
|
@@ -35,16 +38,20 @@ In the first call to `factory`, `A1` is deduced as `int`, so 2 is
|
|
| 35 |
forwarded to `A`’s constructor as an rvalue. In the second call to
|
| 36 |
`factory`, `A1` is deduced as `int&`, so `i` is forwarded to `A`’s
|
| 37 |
constructor as an lvalue. In both cases, `A2` is deduced as `double`, so
|
| 38 |
1.414 is forwarded to `A`’s constructor as an rvalue.
|
| 39 |
|
|
|
|
|
|
|
| 40 |
``` cpp
|
| 41 |
template <class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;
|
| 42 |
```
|
| 43 |
|
| 44 |
*Returns:* `static_cast<remove_reference_t<T>&&>(t)`.
|
| 45 |
|
|
|
|
|
|
|
| 46 |
``` cpp
|
| 47 |
template <class T, class A1>
|
| 48 |
shared_ptr<T> factory(A1&& a1) {
|
| 49 |
return shared_ptr<T>(new T(std::forward<A1>(a1)));
|
| 50 |
}
|
|
@@ -67,13 +74,15 @@ forwarded as a non-const lvalue. This binds to the constructor
|
|
| 67 |
`A(const A&)`, which copies the value from `a`. In the second call to
|
| 68 |
`factory`, because of the call `std::move(a)`, `A1` is deduced as `A`,
|
| 69 |
so `a` is forwarded as an rvalue. This binds to the constructor
|
| 70 |
`A(A&&)`, which moves the value from `a`.
|
| 71 |
|
|
|
|
|
|
|
| 72 |
``` cpp
|
| 73 |
template <class T> constexpr conditional_t<
|
| 74 |
-
|
| 75 |
-
|
| 76 |
```
|
| 77 |
|
| 78 |
-
*Returns:* `std::move(x)`
|
| 79 |
|
|
|
|
| 1 |
+
### Forward/move helpers <a id="forward">[[forward]]</a>
|
| 2 |
|
| 3 |
The library provides templated helper functions to simplify applying
|
| 4 |
move semantics to an lvalue and to simplify the implementation of
|
| 5 |
+
forwarding functions. All functions specified in this subclause are
|
| 6 |
+
signal-safe ([[csignal.syn]]).
|
| 7 |
|
| 8 |
``` cpp
|
| 9 |
template <class T> constexpr T&& forward(remove_reference_t<T>& t) noexcept;
|
| 10 |
template <class T> constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
|
| 11 |
```
|
| 12 |
|
| 13 |
*Returns:* `static_cast<T&&>(t)`.
|
| 14 |
|
| 15 |
+
*Remarks:* If the second form is instantiated with an lvalue reference
|
| 16 |
+
type, the program is ill-formed.
|
| 17 |
+
|
| 18 |
+
[*Example 1*:
|
| 19 |
|
| 20 |
``` cpp
|
| 21 |
template <class T, class A1, class A2>
|
| 22 |
shared_ptr<T> factory(A1&& a1, A2&& a2) {
|
| 23 |
return shared_ptr<T>(new T(std::forward<A1>(a1), std::forward<A2>(a2)));
|
|
|
|
| 38 |
forwarded to `A`’s constructor as an rvalue. In the second call to
|
| 39 |
`factory`, `A1` is deduced as `int&`, so `i` is forwarded to `A`’s
|
| 40 |
constructor as an lvalue. In both cases, `A2` is deduced as `double`, so
|
| 41 |
1.414 is forwarded to `A`’s constructor as an rvalue.
|
| 42 |
|
| 43 |
+
— *end example*]
|
| 44 |
+
|
| 45 |
``` cpp
|
| 46 |
template <class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;
|
| 47 |
```
|
| 48 |
|
| 49 |
*Returns:* `static_cast<remove_reference_t<T>&&>(t)`.
|
| 50 |
|
| 51 |
+
[*Example 2*:
|
| 52 |
+
|
| 53 |
``` cpp
|
| 54 |
template <class T, class A1>
|
| 55 |
shared_ptr<T> factory(A1&& a1) {
|
| 56 |
return shared_ptr<T>(new T(std::forward<A1>(a1)));
|
| 57 |
}
|
|
|
|
| 74 |
`A(const A&)`, which copies the value from `a`. In the second call to
|
| 75 |
`factory`, because of the call `std::move(a)`, `A1` is deduced as `A`,
|
| 76 |
so `a` is forwarded as an rvalue. This binds to the constructor
|
| 77 |
`A(A&&)`, which moves the value from `a`.
|
| 78 |
|
| 79 |
+
— *end example*]
|
| 80 |
+
|
| 81 |
``` cpp
|
| 82 |
template <class T> constexpr conditional_t<
|
| 83 |
+
!is_nothrow_move_constructible_v<T> && is_copy_constructible_v<T>, const T&, T&&>
|
| 84 |
+
move_if_noexcept(T& x) noexcept;
|
| 85 |
```
|
| 86 |
|
| 87 |
+
*Returns:* `std::move(x)`.
|
| 88 |
|