Skip to content

Commit 05294b6

Browse files
committed
add std::start_lifetime_as() and std::start_lifetime_as_array(). (#1409)
1 parent cdd2573 commit 05294b6

File tree

6 files changed

+177
-4
lines changed

6 files changed

+177
-4
lines changed

lang/cpp20/implicit_creation_of_objects_for_low-level_object_manipulation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ unique_ptr<char[]> Stream::read() {
403403

404404
## <a id="relative-page" href="#relative-page">関連項目</a>
405405

406-
- [`start_lifetime_as()`](/reference/memory/start_lifetime_as.md.nolink)
407-
- [`start_lifetime_as_array()`](/reference/memory/start_lifetime_as_array.md.nolink)
406+
- [`start_lifetime_as()`](/reference/memory/start_lifetime_as.md)
407+
- [`start_lifetime_as_array()`](/reference/memory/start_lifetime_as_array.md)
408408
- [`is_implicit_lifetime`](/reference/type_traits/is_implicit_lifetime.md)
409409

410410
## 参照

lang/cpp23.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ C++23とは、2023年中に改訂され、ISO/IEC 14882:2024で標準規格化
201201
### メモリ
202202
- [`<memory>`](/reference/memory.md)に、レガシーC関数からスマートポインタへの直接出力をサポートする、スマートポインタアダプタ[`std::out_ptr`](/reference/memory/out_ptr.md)[`std::inout_ptr`](/reference/memory/inout_ptr.md)を追加
203203
- [`std::unique_ptr`](/reference/memory/unique_ptr.md)クラスを`constexpr`に対応
204-
- [`<memory>`](/reference/memory.md)に、オブジェクトの生存期間を開始することを明示する関数として、[`std::start_lifetime_as()`](/reference/memory/start_lifetime_as.md.nolink)[`std::start_lifetime_as_array()`](/reference/memory/start_lifetime_as_array.md.nolink)を追加
204+
- [`<memory>`](/reference/memory.md)に、オブジェクトの生存期間を開始することを明示する関数として、[`std::start_lifetime_as()`](/reference/memory/start_lifetime_as.md)[`std::start_lifetime_as_array()`](/reference/memory/start_lifetime_as_array.md)を追加
205205
- [`<bit>`](/reference/bit.md)に、値のバイト入れ替え (エンディアン変換) を行う[`std::byteswap()`](/reference/bit/byteswap.md)関数を追加
206206

207207

lang/cpp23/feature_test_macros.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
|`__cpp_lib_shift`|`202202L`|[`std::ranges::shift_left()`](/reference/algorithm/ranges_shift_left.md)[`std::ranges::shift_right()`](/reference/algorithm/ranges_shift_right.md)の追加|[`<algorithm>`](/reference/algorithm.md)|
9090
|`__cpp_lib_spanstream`|`202106L`|`<spanstream>`の追加|`<spanstream>`|
9191
|`__cpp_lib_stacktrace`|`202011L`|[`<stacktrace>`](/reference/stacktrace.md)の追加|[`<stacktrace>`](/reference/stacktrace.md)|
92-
|`__cpp_lib_start_lifetime_as`|`202207L`|`std::start_lifetime_as()`の追加|[`<memory>`](/reference/memory.md)|
92+
|`__cpp_lib_start_lifetime_as`|`202207L`|[`std::start_lifetime_as()`](/reference/memory/start_lifetime_as.md)の追加|[`<memory>`](/reference/memory.md)|
9393
|`__cpp_lib_stdatomic_h`|`202011L`|[`<stdatomic.h>`](/reference/stdatomic.h.md)の追加|[`<stdatomic.h>`](/reference/stdatomic.h.md)|
9494
|`__cpp_lib_string_contains`|`202011L`|[`std::basic_string::contains()`](/reference/string/basic_string/contains.md)[`std::basic_string_view::contains()`](/reference/string_view/basic_string_view/contains.md)の追加|[`<string>`](/reference/string.md)<br/>[`<string_view>`](/reference/string_view.md)|
9595
|`__cpp_lib_string_resize_and_overwrite`|`202110L`|[`std::basic_string::resize_and_overwrite()`](/reference/string/basic_string/resize_and_overwrite.md)の追加|[`<string>`](/reference/string.md)|

reference/memory.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@
131131
| [`align`](memory/align.md) | アライメント調整された領域を得る(function) | C++11 |
132132
| [`assume_aligned`](memory/assume_aligned.md) | コンパイラへアライメントのヒントを与える(function template) | C++20 |
133133
| [`is_sufficiently_aligned`](memory/is_sufficiently_aligned.md) | ポインタのアライメントを判定する(function template) | C++26 |
134+
| [`start_lifetime_as`](memory/start_lifetime_as.md) | オブジェクトの生存期間を開始することを明示する(function template) | C++23 |
135+
| [`start_lifetime_as_array`](memory/start_lifetime_as_array.md) | 配列オブジェクトの生存期間を開始することを明示する(function template) | C++23 |
134136

135137

136138
## ガベージコレクション支援(C++23で削除)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# start_lifetime_as
2+
* memory[meta header]
3+
* function template[meta id-type]
4+
* std[meta namespace]
5+
* cpp23[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template <class T>
10+
T* start_lifetime_as(void* p) noexcept; // (1)
11+
12+
template <class T>
13+
const T* start_lifetime_as(const void* p) noexcept; // (2)
14+
15+
template <class T>
16+
volatile T* start_lifetime_as(volatile void* p) noexcept; // (3)
17+
18+
template <class T>
19+
const volatile T* start_lifetime_as(const volatile void* p) noexcept; // (4)
20+
}
21+
```
22+
23+
## 概要
24+
オブジェクトの生存期間を開始することを明示する。
25+
26+
27+
## 要件
28+
`T` が *implicit-lifetime types* であること。また、`T` が 完全型であること。
29+
30+
31+
## 事前条件
32+
- `[p, (char*)p + sizeof(T))` の範囲が `p` を通じてアクセス可能な領域であること。
33+
- `p` が型 `T` のアライメントを満たすこと。
34+
35+
36+
## 効果
37+
型 `T` のオブジェクト(アドレスは `p`)が暗黙的に構築される。`T` 内にネストされているオブジェクトも暗黙的に構築される。
38+
39+
40+
## 戻り値
41+
暗黙的に構築された型 `T` のオブジェクトのポインタを返す。ポインタの値は `p`。
42+
43+
44+
## 例外
45+
投げない。
46+
47+
48+
## 例
49+
```cpp example
50+
#include <cstdint>
51+
#include <memory>
52+
53+
int main() {
54+
float f = 3.14f;
55+
56+
std::uint32_t* n = std::start_lifetime_as<std::uint32_t>(&f);
57+
58+
std::cout << *n << std::endl;
59+
}
60+
```
61+
* std::start_lifetime_as[color ff0000]
62+
63+
### 出力
64+
```
65+
1078523331
66+
```
67+
68+
69+
## バージョン
70+
### 言語
71+
- C++23
72+
73+
### 処理系
74+
- [Clang](/implementation.md#clang): ??
75+
- [GCC](/implementation.md#gcc): ??
76+
- [Visual C++](/implementation.md#visual_cpp): ??
77+
78+
79+
## 関連項目
80+
- [`std::start_lifetime_as_array`](/reference/memory/start_lifetime_as_array.md)
81+
82+
83+
## 参照
84+
- [P2590R2: Explicit lifetime management](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2590r2.pdf)
85+
- [P2679R2: Fixing std::start_lifetime_as and std::start_lifetime_as_array](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2679r2.pdf)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# start_lifetime_as_array
2+
* memory[meta header]
3+
* function template[meta id-type]
4+
* std[meta namespace]
5+
* cpp23[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template <class T>
10+
T* start_lifetime_as_array(void* p, size_t n) noexcept; // (1)
11+
12+
template <class T>
13+
const T* start_lifetime_as_array(const void* p, size_t n) noexcept; // (2)
14+
15+
template <class T>
16+
volatile T* start_lifetime_as_array(volatile void* p, size_t n) noexcept; // (3)
17+
18+
template <class T>
19+
const volatile T* start_lifetime_as_array(const volatile void* p, size_t n) noexcept; // (4)
20+
}
21+
```
22+
23+
## 概要
24+
配列オブジェクトの生存期間を開始することを明示する。
25+
26+
27+
## 要件
28+
`T` が完全型であること。
29+
30+
31+
## 事前条件
32+
- `p` が、型 `T` の配列のアライメントを満たすこと。または、`p` がnullであること。
33+
- `n <= size_t(-1) / sizeof(T)` が真であること。
34+
- `n > 0` が真ならば、`[(char*)p, (char*)p + (n * sizeof(T)))` の範囲が `p` を通じてアクセス可能な領域であること。
35+
36+
37+
## 効果
38+
`n > 0` が真ならば、サイズ `n` の `T` の配列の型を `U` とすると、[`start_lifetime_as`](/reference/memory/start_lifetime_as.md)`<U>(p)` と同じ効果。そうでなければ、何もしない。
39+
40+
41+
## 戻り値
42+
`n > 0` が真ならば、暗黙的に構築されたサイズ `n` の `T` の配列の、最初の要素へのポインタを返す。そうでなければ、`p`。
43+
44+
45+
## 例外
46+
投げない。
47+
48+
49+
## 例
50+
```cpp example
51+
#include <cstdint>
52+
#include <memory>
53+
54+
int main() {
55+
float f = 3.14f;
56+
57+
std::uint16_t* n = std::start_lifetime_as_array<std::uint16_t>(&f, 2);
58+
59+
std::cout << n[0] << " " << n[1] << std::endl;
60+
}
61+
```
62+
* std::start_lifetime_as_array[color ff0000]
63+
64+
### 出力
65+
```
66+
62915 16456
67+
```
68+
69+
70+
## バージョン
71+
### 言語
72+
- C++23
73+
74+
### 処理系
75+
- [Clang](/implementation.md#clang): ??
76+
- [GCC](/implementation.md#gcc): ??
77+
- [Visual C++](/implementation.md#visual_cpp): ??
78+
79+
80+
## 関連項目
81+
- [`std::start_lifetime_as`](/reference/memory/start_lifetime_as.md)
82+
83+
84+
## 参照
85+
- [P2590R2: Explicit lifetime management](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2590r2.pdf)
86+
- [P2679R2: Fixing std::start_lifetime_as and std::start_lifetime_as_array](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2679r2.pdf)

0 commit comments

Comments
 (0)