Skip to content

Commit

Permalink
expected/expected: value_or,error_or(#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohhoy committed Feb 3, 2023
1 parent 991f1ac commit 195bd06
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 6 deletions.
4 changes: 2 additions & 2 deletions reference/expected/expected.md
Expand Up @@ -53,8 +53,8 @@ namespace std {
| [`has_value`](expected/has_value.md) | 正常値を保持しているかを判定する | C++23 |
| [`value`](expected/value.md) | 正常値を取得する | C++23 |
| [`error`](expected/error.md) | エラー値を取得する | C++23 |
| [`value_or`](expected/value_or.md.nolink) | 正常値もしくは指定された値を取得する | C++23 |
| [`error_or`](expected/error_or.md.nolink) | エラー値もしくは指定された値を取得する | C++23 |
| [`value_or`](expected/value_or.md) | 正常値もしくは指定された値を取得する | C++23 |
| [`error_or`](expected/error_or.md) | エラー値もしくは指定された値を取得する | C++23 |
### モナド操作
Expand Down
2 changes: 1 addition & 1 deletion reference/expected/expected/error.md
Expand Up @@ -67,7 +67,7 @@ ERR


## 関連項目
- [`error_or`](error_or.md.nolink)
- [`error_or`](error_or.md)


## 参照
Expand Down
68 changes: 68 additions & 0 deletions reference/expected/expected/error_or.md
@@ -0,0 +1,68 @@
# error_or
* expected[meta header]
* function template[meta id-type]
* std[meta namespace]
* expected[meta class]
* cpp23[meta cpp]

```cpp
template<class G = E> constexpr T error_or(G&& e) const &; // (1)
template<class G = E> constexpr T error_or(G&& e) &&; // (2)
```
## 概要
エラー値もしくは指定された値を取得する。
## 適格要件
- (1) : [`is_copy_constructible_v`](/reference/type_traits/is_copy_constructible.md)`<E> == true &&` [`is_convertible_v`](/reference/type_traits/is_convertible.md)`<G, E> == true`
- (2) : [`is_move_constructible_v`](/reference/type_traits/is_move_constructible.md)`<E> == true &&` [`is_convertible_v`](/reference/type_traits/is_convertible.md)`<G, E> == true`
## 戻り値
- (1) : [`has_value()`](has_value.md) `?` [`std::forward`](/reference/utility/forward.md)`<G>(e) :` [`error()`](error.md)
- (2) : [`has_value()`](has_value.md) `?` [`std::forward`](/reference/utility/forward.md)`<G>(e) :` [`std::move`](/reference/utility/move.md)`(`[`error()`](error.md)`)`
## 例
```cpp example
#include <expected>
#include <iostream>
#include <string>
int main()
{
std::expected<int, std::string> x = 42;
std::cout << x.error_or("-") << std::endl;
std::expected<int, std::string> y = std::unexpected{"ERR"};
std::cout << y.error_or("-") << std::endl;
}
```
* error_or()[color ff0000]
* std::unexpected[link ../unexpected.md]

### 出力
```
-
ERR
```


## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): 13.0
- [ICC](/implementation.md#icc): ??
- [Visual C++](/implementation.md#visual_cpp): ??


## 関連項目
- [`error()`](error.md)


## 参照
- [P0323R12 std::expected](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0323r12.html)
2 changes: 1 addition & 1 deletion reference/expected/expected/op_arrow.md
Expand Up @@ -65,7 +65,7 @@ int main()
## 関連項目
- [`operator*`](op_deref.md)
- [`value()`](value.md)
- [`value_or()`](value_or.md.nolink)
- [`value_or()`](value_or.md)


## 参照
Expand Down
2 changes: 1 addition & 1 deletion reference/expected/expected/op_deref.md
Expand Up @@ -70,7 +70,7 @@ int main()
## 関連項目
- [`operator->`](op_arrow.md)
- [`value()`](value.md)
- [`value_or()`](value_or.md.nolink)
- [`value_or()`](value_or.md)


## 参照
Expand Down
2 changes: 1 addition & 1 deletion reference/expected/expected/value.md
Expand Up @@ -73,7 +73,7 @@ throw:ERR
## 関連項目
- [`operator->`](op_arrow.md)
- [`operator*`](op_deref.md)
- [`value_or()`](value_or.md.nolink)
- [`value_or()`](value_or.md)


## 参照
Expand Down
70 changes: 70 additions & 0 deletions reference/expected/expected/value_or.md
@@ -0,0 +1,70 @@
# value_or
* expected[meta header]
* function template[meta id-type]
* std[meta namespace]
* expected[meta class]
* cpp23[meta cpp]

```cpp
template<class U> constexpr T value_or(U&& v) const &; // (1)
template<class U> constexpr T value_or(U&& v) &&; // (2)
```
## 概要
正常値もしくは指定された値を取得する。
## 適格要件
- (1) : [`is_copy_constructible_v`](/reference/type_traits/is_copy_constructible.md)`<T> == true &&` [`is_convertible_v`](/reference/type_traits/is_convertible.md)`<U, T> == true`
- (2) : [`is_move_constructible_v`](/reference/type_traits/is_move_constructible.md)`<T> == true &&` [`is_convertible_v`](/reference/type_traits/is_convertible.md)`<U, T> == true`
## 戻り値
- (1) : [`has_value()`](has_value.md) `?` [`**this`](op_deref.md) `: static_cast<T>(`[`std::forward`](/reference/utility/forward.md)`<U>(v))`
- (2) : [`has_value()`](has_value.md) `?` [`std::move`](/reference/utility/move.md)`(`[`**this`](op_deref.md)`) : static_cast<T>(`[`std::forward`](/reference/utility/forward.md)`<U>(v))`
## 例
```cpp example
#include <expected>
#include <iostream>
#include <string>
int main()
{
std::expected<int, std::string> x = 42;
std::cout << x.value_or(0) << std::endl;
std::expected<int, std::string> y = std::unexpected{"ERR"};
std::cout << y.value_or(0) << std::endl;
}
```
* value_or()[color ff0000]
* std::unexpected[link ../unexpected.md]

### 出力
```
42
0
```


## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): 16.0
- [GCC](/implementation.md#gcc): 12.1
- [ICC](/implementation.md#icc): ??
- [Visual C++](/implementation.md#visual_cpp): ??


## 関連項目
- [`operator->`](op_arrow.md)
- [`operator*`](op_deref.md)
- [`value()`](value.md)


## 参照
- [P0323R12 std::expected](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0323r12.html)

0 comments on commit 195bd06

Please sign in to comment.