Skip to content

Commit b5dd43f

Browse files
committed
memory: is_sufficiently_aligned P2897R7(#1380)
1 parent 000cd8f commit b5dd43f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

reference/memory/assume_aligned.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ C++20 から標準で使用可能となる本機能だが、実装例からも
345345
346346
- [`alignas`](../../lang/cpp11/alignas.md)
347347
- [`alignof`](../../lang/cpp11/alignof.md)
348+
- [`is_sufficiently_aligned`](is_sufficiently_aligned.md)
348349
349350
## 参照
350351
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# is_sufficiently_aligned
2+
* memory[meta header]
3+
* function template[meta id-type]
4+
* std[meta namespace]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
template<size_t Alignment, class T>
9+
bool is_sufficiently_aligned(T* ptr);
10+
```
11+
12+
## 概要
13+
ポインタ値が指定したアライメントを満たすか否かを返す。
14+
15+
16+
## 事前条件
17+
ポインタ`p`が、`T`に類似(similar)した型のオブジェクト`X`を指すこと。
18+
19+
20+
## 戻り値
21+
`X`が少なくとも`Alignment`でアライメントされるならば`true`。そうでなければ`false`。
22+
23+
24+
## 例外
25+
投げない
26+
27+
28+
## この機能が必要になった背景・経緯
29+
この関数テンプレートは[`<mdspan>`](/reference/mdspan.md)ヘッダへの[`aligned_accessor`](/reference/mdspan/aligned_accessor.md.nolink)導入に伴って必要とされた機能である。
30+
一方で、ポインタ値のアライメント要件を検査するユースケースは一般的と考えられたため、汎用ユーティリティとして`<memory>`ヘッダに対して機能追加された。
31+
32+
33+
## 例
34+
```cpp example
35+
#include <cassert>
36+
#include <memory>
37+
#include <news>
38+
39+
int main()
40+
{
41+
int *ptr = new(std::align_val_t{32}) int;
42+
assert( std::is_sufficiently_aligned<32>(ptr) );
43+
delete ptr;
44+
}
45+
```
46+
* std::is_sufficiently_aligned[color ff0000]
47+
* std::align_val_t[link /reference/new/align_val_t.md]
48+
49+
### 出力
50+
```
51+
```
52+
53+
54+
## 実装例
55+
```cpp
56+
// 提案文書P2897R7より引用
57+
template<size_t ByteAlignment, class ElementType>
58+
bool is_sufficiently_aligned(ElementType* p)
59+
{
60+
return bit_cast<uintptr_t>(p) % ByteAlignment == 0;
61+
}
62+
```
63+
* bit_cast[link /reference/bit/bit_cast.md]
64+
65+
66+
## バージョン
67+
### 言語
68+
- C++26
69+
70+
### 処理系
71+
- [Clang](/implementation.md#clang): ??
72+
- [GCC](/implementation.md#gcc): ??
73+
- [Visual C++](/implementation.md#visual_cpp): ??
74+
75+
76+
## 関連項目
77+
- [`assume_aligned`](assume_aligned.md)
78+
- [アライメント指定されたデータの動的メモリ確保](/lang/cpp17/dynamic_memory_allocation_for_over-aligned_data.md)
79+
80+
81+
## 参照
82+
- [P2897R7 `aligned_accessor`: An mdspan accessor expressing pointer over-alignment](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2897r7.html)

0 commit comments

Comments
 (0)