Skip to content

Commit a719124

Browse files
committed
P0556R3
1 parent 1a111e0 commit a719124

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

docs/standardization/cpp20.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,3 +1379,68 @@ int main()
13791379
```
13801380

13811381
`std::move()` を使うこの仕様変更は、同じ `<numeric>` ヘッダの `std::inner_product()``std::partial_sum()`, `std::adjacent_difference()` にも適用されます。
1382+
1383+
1384+
### 2 の累乗数に関するユーティリティ関数を追加 [(P0556R3)](https://wg21.link/P0556R3), [(P1355R2)](https://wg21.link/P1355R2)
1385+
ある整数が 2 の累乗数であるかを調べたり、ある整数に近い 2 の累乗数を探したりする処理は、プログラミングでよく登場しますが、C++17 の標準ライブラリには実装されていませんでした。C++20 では次のような関数が追加されます。
1386+
1387+
```C++
1388+
namespace std
1389+
{
1390+
template <class T>
1391+
constexpr bool ispow2(T x) noexcept;
1392+
1393+
template <class T>
1394+
constexpr T ceil2(T x);
1395+
1396+
template <class T>
1397+
constexpr T floor2(T x) noexcept;
1398+
1399+
template <class T>
1400+
constexpr T log2p1(T x) noexcept;
1401+
}
1402+
```
1403+
1404+
- 引数 `x` が 2 の累乗数であるかを判定する `std::ispow2(x)`
1405+
- `x` 以上で最小の 2 の累乗数を返す `std::ceil2(x)`
1406+
- `x` 以下で最大の 2 の累乗数 (ただし `x` が 0 の場合は 0) を返す `std::floor2(x)`)
1407+
- `(1 + log2(x))` の整数部 (ただし `x` が 0 の場合は 0) を返す `std::log2p1(x)`
1408+
-`x` を表現するために何ビット必要か」と同じ意味
1409+
1410+
いずれの関数も、型 `T` が符号なし整数型 (`unsigned char`, `unsigned short`, `unsigned int`, `unsigned long`, `unsigned long long`) の場合のみオーバーロード解決に参加します。なお、`std::ceil2(x)` について、結果が型 `T` で表現できない場合の動作は未定義です。
1411+
1412+
```C++
1413+
#include <iostream>
1414+
#include <bit>
1415+
1416+
int main()
1417+
{
1418+
for (unsigned i = 0; i <= 16; ++i)
1419+
{
1420+
std::cout << i << ": "
1421+
<< std::ispow2(i) << ' '
1422+
<< std::ceil2(i) << ' '
1423+
<< std::floor2(i) << ' '
1424+
<< std::log2p1(i) << '\n';
1425+
}
1426+
}
1427+
```
1428+
```
1429+
0: 0 1 0 0
1430+
1: 1 1 1 1
1431+
2: 1 2 2 2
1432+
3: 0 4 2 2
1433+
4: 1 4 4 3
1434+
5: 0 8 4 3
1435+
6: 0 8 4 3
1436+
7: 0 8 4 3
1437+
8: 1 8 8 4
1438+
9: 0 16 8 4
1439+
10: 0 16 8 4
1440+
11: 0 16 8 4
1441+
12: 0 16 8 4
1442+
13: 0 16 8 4
1443+
14: 0 16 8 4
1444+
15: 0 16 8 4
1445+
16: 1 16 16 5
1446+
```

docs/tools/compilers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
description: C++ プログラムのコンパイラの紹介
1+
description: C++ コンパイラの紹介
22

33
# C++ コンパイラ
44

0 commit comments

Comments
 (0)