Skip to content

Commit 0e6380b

Browse files
committed
cpp26/contracts : P2900R14に合わせて修正
1 parent 5786bf0 commit 0e6380b

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

lang/cpp26/contracts.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ public:
7676
#### 事後条件(post)
7777
関数の実行後に満たされているべき条件を指定する。
7878
```cpp
79-
int increment(int x)
79+
int increment(const int x)
8080
post(r: r == x + 1)
8181
{
8282
return x + 1;
8383
}
8484
```
8585
ここでは、`increment`関数の戻り値が`x + 1`であることを事後条件として指定している。
86+
事後条件に関数引数を使用する場合、使用する引数は参照型か、`const`修飾型でなければならない。
8687
8788
`post`では、戻り値を`r`としてバインドし、条件式内で利用している。ここには、任意の変数名が使用できる。変数は定数(`const`)な左辺値参照である。
8889
@@ -443,9 +444,9 @@ C++26では、契約プログラミングをサポートするために`<contrac
443444
```cpp
444445
namespace std::contracts {
445446
enum class assertion_kind {
446-
precondition, // 事前条件アサーション
447-
postcondition, // 事後条件アサーション
448-
assertion // アサーション文
447+
pre, // 事前条件アサーション
448+
post, // 事後条件アサーション
449+
assert // アサーション文
449450
};
450451
}
451452
```
@@ -485,9 +486,9 @@ namespace std::contracts {
485486
public:
486487
assertion_kind kind() const noexcept;
487488
evaluation_semantic semantic() const noexcept;
488-
detection_mode detection() const noexcept;
489+
contracts::detection_mode detection_mode() const noexcept;
489490
source_location location() const noexcept;
490-
string_view comment() const noexcept;
491+
const char* comment() const noexcept;
491492
bool is_terminating() const noexcept;
492493
exception_ptr evaluation_exception() const noexcept;
493494
};
@@ -497,7 +498,7 @@ namespace std::contracts {
497498
主なメンバ関数:
498499
- `kind()`: 違反した契約アサーションの種類を返す
499500
- `semantic()`: 使用された評価セマンティクスを返す
500-
- `detection()`: 違反の検出方法を返す
501+
- `detection_mode()`: 違反の検出方法を返す
501502
- `location()`: 契約アサーションのソースロケーションを返す
502503
- `comment()`: ベンダー固有のコメント文字列を返す
503504
- `is_terminating()`: 違反後にプログラムが終了するかどうかを返す
@@ -559,7 +560,7 @@ void f(int x) pre(might_throw(x)); // 例外発生時は契約違反
559560
#include <iostream>
560561

561562
// 事前条件と事後条件を持つ関数
562-
int safe_division(int numerator, int denominator)
563+
int safe_division(const int numerator, const int denominator)
563564
pre(denominator != 0)
564565
post(result: result * denominator == numerator)
565566
{
@@ -595,7 +596,7 @@ public:
595596
};
596597

597598
// ラムダ式での使用
598-
auto lambda_with_contract = [](int x)
599+
auto lambda_with_contract = [](const int x)
599600
pre(x > 0)
600601
post(r: r > x)
601602
{
@@ -633,13 +634,13 @@ void handle_contract_violation(const std::contracts::contract_violation& v) {
633634
std::cerr << "契約違反が発生しました:\n";
634635
std::cerr << " 種類: ";
635636
switch (v.kind()) {
636-
case std::contracts::assertion_kind::precondition:
637+
case std::contracts::assertion_kind::pre:
637638
std::cerr << "事前条件\n";
638639
break;
639-
case std::contracts::assertion_kind::postcondition:
640+
case std::contracts::assertion_kind::post:
640641
std::cerr << "事後条件\n";
641642
break;
642-
case std::contracts::assertion_kind::assertion:
643+
case std::contracts::assertion_kind::assert:
643644
std::cerr << "アサーション\n";
644645
break;
645646
}
@@ -656,7 +657,7 @@ void handle_contract_violation(const std::contracts::contract_violation& v) {
656657
std::contracts::invoke_default_contract_violation_handler(v);
657658
}
658659
659-
int process(int x)
660+
int process(const int x)
660661
pre(x > 0)
661662
post(r: r > x)
662663
{
@@ -679,7 +680,7 @@ int main() {
679680
#include <vector>
680681

681682
template<std::integral T>
682-
T increment(T value)
683+
T increment(const T value)
683684
pre(value < std::numeric_limits<T>::max())
684685
post(result: result == value + 1)
685686
{

0 commit comments

Comments
 (0)