1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-06 06:42:33 +03:00

Added get_ref()

Same as get_ptr() but for references.
If the type is incompatible it throws (get_ptr() returns null).
Implemented in terms of get_ptr().
This commit is contained in:
dariomt
2015-10-16 15:23:57 +02:00
parent 8f97e99feb
commit bd2783f45c
3 changed files with 273 additions and 2 deletions

26
doc/examples/get_ref.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <json.hpp>
using namespace nlohmann;
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(std::domain_error& ex)
{
std::cout << ex.what() << '\n';
}
}