This commit is contained in:
nlohmann committed 2026-10-12 07:35:31 +00:00
1 parent 79119509a9
commit 22fba27145
555 files changed
+17342 -1394

No files matched your search

+31 -2
View File
@@ -49,13 +49,42 @@ Constant.
The example shows several calls to `get_ref()`.
```cpp
--8<-- "examples/get_ref.cpp"
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create a JSON number
json value = 17;
// explicitly getting references
auto r1 = value.get_ref<const json::number_integer_t&>();
auto r2 = value.get_ref<json::number_integer_t&>();
// print the values
std::cout << r1 << ' ' << r2 << '\n';
// incompatible type throws exception
try
{
auto r3 = value.get_ref<json::number_float_t&>();
}
catch (const json::type_error& ex)
{
std::cout << ex.what() << '\n';
}
}
```
Output:
```json
--8<-- "examples/get_ref.output"
17 17
[json.exception.type_error.303] incompatible ReferenceType for get_ref, actual type is number
```
## See also