mirror of
https://github.com/nlohmann/json.git
synced 2025-08-06 16:39:26 +08:00
Add note on derived return type for value function (#4628)
Some checks failed
Code scanning - action / CodeQL-Build (push) Has been cancelled
macOS / macos-13 (14.1) (push) Has been cancelled
macOS / macos-13 (14.2) (push) Has been cancelled
macOS / macos-13 (14.3) (push) Has been cancelled
macOS / macos-13 (14.3.1) (push) Has been cancelled
macOS / macos-13 (15.0.1) (push) Has been cancelled
macOS / macos-13 (15.1) (push) Has been cancelled
macOS / macos-13 (15.2) (push) Has been cancelled
macOS / macos-14 (15.3) (push) Has been cancelled
macOS / macos-14 (15.4) (push) Has been cancelled
macOS / macos-15 (16.0) (push) Has been cancelled
macOS / macos-15 (16.1) (push) Has been cancelled
macOS / macos-15 (16.2) (push) Has been cancelled
macOS / xcode_standards (11) (push) Has been cancelled
macOS / xcode_standards (14) (push) Has been cancelled
macOS / xcode_standards (17) (push) Has been cancelled
macOS / xcode_standards (20) (push) Has been cancelled
macOS / xcode_standards (23) (push) Has been cancelled
macOS / xcode_standards (26) (push) Has been cancelled
Publish documentation / publish_documentation (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Ubuntu / ci_test_gcc (push) Has been cancelled
Ubuntu / ci_infer (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_cppcheck) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_cpplint) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_non_git_tests) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_offline_testdata) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_reproducible_tests) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_reuse_compliance) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_test_amalgamation) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_test_single_header) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_test_valgrind) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_clang_analyze) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_clang_tidy) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_single_binaries) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_test_clang) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_test_clang_sanitizer) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_cmake_flags) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_diagnostic_positions) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_diagnostics) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_legacycomparison) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_noexceptions) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_noglobaludls) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_noimplicitconversions) (push) Has been cancelled
Ubuntu / ci_test_coverage (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc_old (4.8) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc_old (4.9) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc_old (5) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc_old (6) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (10) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (11) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (12) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (13) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (14) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (7) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (8) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (9) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (latest) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (10) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (11) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (12) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (13) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (14) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (15-bullseye) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (16) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (17) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (18) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (19) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.4) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.5) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.6) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.7) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.8) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.9) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (4) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (5) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (6) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (7) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (8) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (9) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (latest) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (11) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (14) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (17) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (20) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (23) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (26) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (11, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (11, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (14, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (14, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (17, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (17, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (20, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (20, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (23, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (23, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (26, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (26, libstdcxx) (push) Has been cancelled
Ubuntu / ci_cuda_example (push) Has been cancelled
Ubuntu / ci_icpc (push) Has been cancelled
Ubuntu / ci_test_documentation (ci_test_build_documentation) (push) Has been cancelled
Ubuntu / ci_test_documentation (ci_test_examples) (push) Has been cancelled
Windows / mingw (x64) (push) Has been cancelled
Windows / mingw (x86) (push) Has been cancelled
Windows / msvc2019 (Win32, Debug) (push) Has been cancelled
Windows / msvc2019 (Win32, Release) (push) Has been cancelled
Windows / msvc2019 (x64, Debug) (push) Has been cancelled
Windows / msvc2019 (x64, Release) (push) Has been cancelled
Windows / msvc2019_latest (push) Has been cancelled
Windows / msvc2022 (Win32, Debug) (push) Has been cancelled
Windows / msvc2022 (Win32, Release) (push) Has been cancelled
Windows / msvc2022 (x64, Debug) (push) Has been cancelled
Windows / msvc2022 (x64, Release) (push) Has been cancelled
Windows / msvc2022_latest (push) Has been cancelled
Windows / clang (11) (push) Has been cancelled
Windows / clang (12) (push) Has been cancelled
Windows / clang (13) (push) Has been cancelled
Windows / clang (14) (push) Has been cancelled
Windows / clang (15) (push) Has been cancelled
Windows / clang-cl-11 (Win32) (push) Has been cancelled
Windows / clang-cl-11 (x64) (push) Has been cancelled
Some checks failed
Code scanning - action / CodeQL-Build (push) Has been cancelled
macOS / macos-13 (14.1) (push) Has been cancelled
macOS / macos-13 (14.2) (push) Has been cancelled
macOS / macos-13 (14.3) (push) Has been cancelled
macOS / macos-13 (14.3.1) (push) Has been cancelled
macOS / macos-13 (15.0.1) (push) Has been cancelled
macOS / macos-13 (15.1) (push) Has been cancelled
macOS / macos-13 (15.2) (push) Has been cancelled
macOS / macos-14 (15.3) (push) Has been cancelled
macOS / macos-14 (15.4) (push) Has been cancelled
macOS / macos-15 (16.0) (push) Has been cancelled
macOS / macos-15 (16.1) (push) Has been cancelled
macOS / macos-15 (16.2) (push) Has been cancelled
macOS / xcode_standards (11) (push) Has been cancelled
macOS / xcode_standards (14) (push) Has been cancelled
macOS / xcode_standards (17) (push) Has been cancelled
macOS / xcode_standards (20) (push) Has been cancelled
macOS / xcode_standards (23) (push) Has been cancelled
macOS / xcode_standards (26) (push) Has been cancelled
Publish documentation / publish_documentation (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Ubuntu / ci_test_gcc (push) Has been cancelled
Ubuntu / ci_infer (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_cppcheck) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_cpplint) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_non_git_tests) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_offline_testdata) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_reproducible_tests) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_reuse_compliance) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_test_amalgamation) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_test_single_header) (push) Has been cancelled
Ubuntu / ci_static_analysis_ubuntu (ci_test_valgrind) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_clang_analyze) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_clang_tidy) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_single_binaries) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_test_clang) (push) Has been cancelled
Ubuntu / ci_static_analysis_clang (ci_test_clang_sanitizer) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_cmake_flags) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_diagnostic_positions) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_diagnostics) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_legacycomparison) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_noexceptions) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_noglobaludls) (push) Has been cancelled
Ubuntu / ci_cmake_options (ci_test_noimplicitconversions) (push) Has been cancelled
Ubuntu / ci_test_coverage (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc_old (4.8) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc_old (4.9) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc_old (5) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc_old (6) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (10) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (11) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (12) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (13) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (14) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (7) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (8) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (9) (push) Has been cancelled
Ubuntu / ci_test_compilers_gcc (latest) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (10) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (11) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (12) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (13) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (14) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (15-bullseye) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (16) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (17) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (18) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (19) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.4) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.5) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.6) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.7) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.8) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (3.9) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (4) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (5) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (6) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (7) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (8) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (9) (push) Has been cancelled
Ubuntu / ci_test_compilers_clang (latest) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (11) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (14) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (17) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (20) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (23) (push) Has been cancelled
Ubuntu / ci_test_standards_gcc (26) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (11, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (11, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (14, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (14, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (17, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (17, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (20, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (20, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (23, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (23, libstdcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (26, libcxx) (push) Has been cancelled
Ubuntu / ci_test_standards_clang (26, libstdcxx) (push) Has been cancelled
Ubuntu / ci_cuda_example (push) Has been cancelled
Ubuntu / ci_icpc (push) Has been cancelled
Ubuntu / ci_test_documentation (ci_test_build_documentation) (push) Has been cancelled
Ubuntu / ci_test_documentation (ci_test_examples) (push) Has been cancelled
Windows / mingw (x64) (push) Has been cancelled
Windows / mingw (x86) (push) Has been cancelled
Windows / msvc2019 (Win32, Debug) (push) Has been cancelled
Windows / msvc2019 (Win32, Release) (push) Has been cancelled
Windows / msvc2019 (x64, Debug) (push) Has been cancelled
Windows / msvc2019 (x64, Release) (push) Has been cancelled
Windows / msvc2019_latest (push) Has been cancelled
Windows / msvc2022 (Win32, Debug) (push) Has been cancelled
Windows / msvc2022 (Win32, Release) (push) Has been cancelled
Windows / msvc2022 (x64, Debug) (push) Has been cancelled
Windows / msvc2022 (x64, Release) (push) Has been cancelled
Windows / msvc2022_latest (push) Has been cancelled
Windows / clang (11) (push) Has been cancelled
Windows / clang (12) (push) Has been cancelled
Windows / clang (13) (push) Has been cancelled
Windows / clang (14) (push) Has been cancelled
Windows / clang (15) (push) Has been cancelled
Windows / clang-cl-11 (Win32) (push) Has been cancelled
Windows / clang-cl-11 (x64) (push) Has been cancelled
* 📝 add note on derived return type Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 📝 add note on derived return type Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
parent
d0789e365d
commit
e90c860d53
@ -215,8 +215,8 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
||||
## See also
|
||||
|
||||
- documentation on [checked access](../../features/element_access/checked_access.md)
|
||||
- see [`operator[]`](operator%5B%5D.md) for unchecked access by reference
|
||||
- see [`value`](value.md) for access with default value
|
||||
- [`operator[]`](operator%5B%5D.md) for unchecked access by reference
|
||||
- [`value`](value.md) for access with default value
|
||||
|
||||
## Version history
|
||||
|
||||
|
@ -103,6 +103,30 @@ changes to any JSON value.
|
||||
2. Logarithmic in the size of the container.
|
||||
3. Logarithmic in the size of the container.
|
||||
|
||||
## Notes
|
||||
|
||||
!!! warning "Return type"
|
||||
|
||||
The value function is a template, and the return type of the function is determined by the type of the provided
|
||||
default value unless otherwise specified. This can have unexpected effects. In the example below, we store a 64-bit
|
||||
unsigned integer. We get exactly that value when using [`operator[]`](operator[].md). However, when we call `value`
|
||||
and provide `#!c 0` as default value, then `#!c -1` is returned. The occurs, because `#!c 0` has type `#!c int`
|
||||
which overflows when handling the value `#!c 18446744073709551615`.
|
||||
|
||||
To address this issue, either provide a correctly typed default value or use the template parameter to specify the
|
||||
desired return type. Note that this issue occurs even when a value is stored at the provided key, and the default
|
||||
value is not used as the return value.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/value__return_type.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/value__return_type.output"
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
??? example "Example: (1) access specified object element with default value"
|
||||
|
14
docs/mkdocs/docs/examples/value__return_type.cpp
Normal file
14
docs/mkdocs/docs/examples/value__return_type.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
json j = json::parse(R"({"uint64": 18446744073709551615})");
|
||||
|
||||
std::cout << "operator[]: " << j["uint64"] << '\n'
|
||||
<< "default value (int): " << j.value("uint64", 0) << '\n'
|
||||
<< "default value (uint64_t): " << j.value("uint64", std::uint64_t(0)) << '\n'
|
||||
<< "explict return value type: " << j.value<std::uint64_t>("uint64", 0) << '\n';
|
||||
}
|
4
docs/mkdocs/docs/examples/value__return_type.output
Normal file
4
docs/mkdocs/docs/examples/value__return_type.output
Normal file
@ -0,0 +1,4 @@
|
||||
operator[]: 18446744073709551615
|
||||
default value (int): -1
|
||||
default value (uint64_t): 18446744073709551615
|
||||
explict return value type: 18446744073709551615
|
@ -2,7 +2,11 @@
|
||||
|
||||
## Overview
|
||||
|
||||
In many situations such as configuration files, missing values are not exceptional, but may be treated as if a default value was present.
|
||||
In many situations such as configuration files, missing values are not exceptional, but may be treated as if a default
|
||||
value was present. For this case, use [`value(key, default_value)`](../../api/basic_json/value.md) which takes the key
|
||||
you want to access and a default value in case there is no value stored with that key.
|
||||
|
||||
## Example
|
||||
|
||||
??? example
|
||||
|
||||
@ -17,16 +21,43 @@ In many situations such as configuration files, missing values are not exception
|
||||
|
||||
Assume the value is parsed to a `json` variable `j`.
|
||||
|
||||
| expression | value |
|
||||
| ---------- | ----- |
|
||||
| `#!cpp j` | `#!json {"logOutput": "result.log", "append": true}` |
|
||||
| `#!cpp j.value("logOutput", "logfile.log")` | `#!json "result.log"` |
|
||||
| `#!cpp j.value("append", true)` | `#!json true` |
|
||||
| `#!cpp j.value("append", false)` | `#!json true` |
|
||||
| `#!cpp j.value("logLevel", "verbose")` | `#!json "verbose"` |
|
||||
| expression | value |
|
||||
|---------------------------------------------|------------------------------------------------------|
|
||||
| `#!cpp j` | `#!json {"logOutput": "result.log", "append": true}` |
|
||||
| `#!cpp j.value("logOutput", "logfile.log")` | `#!json "result.log"` |
|
||||
| `#!cpp j.value("append", true)` | `#!json true` |
|
||||
| `#!cpp j.value("append", false)` | `#!json true` |
|
||||
| `#!cpp j.value("logLevel", "verbose")` | `#!json "verbose"` |
|
||||
|
||||
## Note
|
||||
## Notes
|
||||
|
||||
!!! failure "Exceptions"
|
||||
|
||||
- `value` can only be used with objects. For other types, a [`basic_json::type_error`](../../home/exceptions.md#jsonexceptiontype_error306) is thrown.
|
||||
|
||||
!!! warning "Return type"
|
||||
|
||||
The value function is a template, and the return type of the function is determined by the type of the provided
|
||||
default value unless otherwise specified. This can have unexpected effects. In the example below, we store a 64-bit
|
||||
unsigned integer. We get exactly that value when using [`operator[]`](../../api/basic_json/operator[].md). However,
|
||||
when we call `value` and provide `#!c 0` as default value, then `#!c -1` is returned. The occurs, because `#!c 0`
|
||||
has type `#!c int` which overflows when handling the value `#!c 18446744073709551615`.
|
||||
|
||||
To address this issue, either provide a correctly typed default value or use the template parameter to specify the
|
||||
desired return type. Note that this issue occurs even when a value is stored at the provided key, and the default
|
||||
value is not used as the return value.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/value__return_type.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/value__return_type.output"
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [`value`](../../api/basic_json/value.md) for access with default value
|
||||
- documentation on [checked access](checked_access.md)
|
||||
|
Loading…
Reference in New Issue
Block a user