mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 00:38:08 +08:00
Implement find_or_default
This commit is contained in:
@@ -544,6 +544,17 @@ This is useful when you want to provide a fallback value instead of handling exc
|
||||
const auto a = toml::find_or(input, "a", 42);
|
||||
```
|
||||
|
||||
## Using `toml::find_or_default` to Search and Use the Default Value on Failure
|
||||
|
||||
`toml::find_or_default` works similarly to `toml::find_or<T>` but returns the default constructor result if the search or conversion fails.
|
||||
|
||||
This is useful when you want to use the default value instead of handling exceptions, especially when the default construction is costly, as this delays it until the actual failure happens.
|
||||
|
||||
```cpp
|
||||
const auto a = toml::find_or(input, "a", expensive()); // ctor is called before the function call
|
||||
const auto a = toml::find_or_default<expensive>(input, "a"); // ctor will be called only on failure
|
||||
```
|
||||
|
||||
## `toml::find<std::optional<T>>`
|
||||
|
||||
If `std::optional` is available, you can specify `std::optional` as a template argument of `toml::find`.
|
||||
|
@@ -569,6 +569,17 @@ const auto a = toml::find_or(input, "a", 42);
|
||||
|
||||
型変換の失敗だけでなく、キーが見つからなかった場合もデフォルト値を返します。
|
||||
|
||||
## `toml::find_or_default`を使って失敗時の値を指定する
|
||||
|
||||
`toml::find_or_default` は、 `toml::find_or` と同様に、失敗時にデフォルトコンストラクタの結果を返します。デフォルトコンストラクタは失敗時のみに呼ばれるため、特にそれが高価な時に有効です。
|
||||
|
||||
```cpp
|
||||
const auto a = toml::find_or(input, "a", expensive()); // 関数呼出前にコンストラクタ呼出
|
||||
const auto a = toml::find_or_default<expensive>(input, "a"); // 失敗時にのみコンストラクタ呼出
|
||||
```
|
||||
|
||||
型変換の失敗だけでなく、キーが見つからなかった場合もデフォルトコンストラクタの結果を返します。
|
||||
|
||||
## `toml::find<std::optional<T>>`
|
||||
|
||||
C++17以降の場合、`std::optional`を`toml::find`に指定することができます。
|
||||
|
Reference in New Issue
Block a user