Skip to content

Commit 72e5da3

Browse files
committed
P0722R3
1 parent 72e5fd6 commit 72e5da3

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

docs/standardization/cpp20.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,7 @@ int main()
15401540
### `std::function` のムーブコンストラクタが `noexcept`[(P0771R1)](https://wg21.link/P0771R1)
15411541
実行時性能を向上させるため、C++20 では `std::function` のムーブコンストラクタが `noexcept` になります。なお、libstdc++ と libc++ では提案時点ですでに実装済みでした。
15421542

1543+
15431544
### `<utility>`, `<algorithm>` の多くの関数が `constexpr`[(P0202R3)](https://wg21.link/P0202R3), [(P0879R0)](https://wg21.link/P0879R0)
15441545
C++20 では `<utility>` ヘッダの `std::swap()`, `std::exchange()` および、`<algorithm>` ヘッダで条件を満たす全関数が `constexpr` に対応します。`std::all_of()``std::sort()`, `std::reverse()` など、よく使われるアルゴリズム関数が `constexpr` になります。
15451546

@@ -1575,4 +1576,57 @@ int main()
15751576
15761577
```
15771578
7
1578-
```
1579+
```
1580+
1581+
1582+
### デストラクタを自動で呼ばない `delete` 演算子オーバーロード [(P0722R3)](https://wg21.link/P0722R3)
1583+
C++17 では、ユーザ定義の `delete` の中でメンバ変数にアクセスしたくても、すでにクラスのデストラクタが呼ばれているため未定義の動作になります。
1584+
1585+
```C++
1586+
#include <iostream>
1587+
1588+
struct Object
1589+
{
1590+
std::string m_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1591+
1592+
static void operator delete(void* p)
1593+
{
1594+
std::cout << "m_str: "
1595+
<< static_cast<Object*>(p)->m_str // 未定義の動作
1596+
<< '\n';
1597+
::operator delete(p);
1598+
}
1599+
};
1600+
1601+
int main()
1602+
{
1603+
Object* p = new Object();
1604+
delete p;
1605+
}
1606+
```
1607+
1608+
C++20 では、`delete` の第二引数を `std::destroying_delete_t` 型にすることで、デストラクタの呼び出しが自動で実行されない挙動に設定できます(この新しい挙動を「destroying operator delete」と呼びます)。`void T::operator delete(T* ptr, std::destroying_delete_t)` のように、第一引数にはクラスへのポインタが渡され、明示的にデストラクタ `ptr->~T()` を呼ぶ必要があります。
1609+
1610+
```C++
1611+
#include <iostream>
1612+
1613+
struct Object
1614+
{
1615+
std::string m_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1616+
1617+
static void operator delete(Object* p, std::destroying_delete_t)
1618+
{
1619+
std::cout << "m_str: "
1620+
<< p->m_str
1621+
<< '\n';
1622+
p->~Object();
1623+
::operator delete(p);
1624+
}
1625+
};
1626+
1627+
int main()
1628+
{
1629+
Object* p = new Object();
1630+
delete p;
1631+
}
1632+
```

0 commit comments

Comments
 (0)