Skip to content

get_ref<const Json::number_integer_t&> throws exception in 3.12.0 but not in 3.11.1 #4831

@marwanramadan

Description

@marwanramadan

Description

When i compile the following code with the 3.12.0 it will throw an exception, compiled with the 3.11.1 the expected result will be print out.
Is this a bug in 3.12.0 or is this a misuse of the API from my side?

Reproduction steps

Compiled with:
clang++ t1.cpp -o t1

Expected vs. actual results

The expected result is:
t2=3
With 3.12.0 i see this instead:
Exception: [json.exception.type_error.303] incompatible ReferenceType for get_ref, actual type is number

Minimal code example

//#include "json-3.12.0.hpp"
#include "json-3.11.1.hpp"
#include <iostream>

using Json = nlohmann::basic_json<>;

int main() try
{
    const Json test_js = Json::parse(R"({ "t2":3 })");

    if(const auto iter{test_js.find("t2")}; iter != test_js.end())
    {
        auto ref = iter->get_ref<const Json::number_integer_t&>();
        std::cout << "t2=" << ref << '\n';
    }
    else
    {
        std::cerr << "Can't find 't2'\n";
    }
    return 0;
}
catch(const std::exception& e)
{
    std::cerr << "Exception: " << e.what() << '\n';
}

Error messages

Compiler and operating system

Linux Manjaro, clang 20.1.6 (Manjaro Repo), g++ 15.1.1 (Manjaro Repo)

Library version

3.11.1 and 3.12.0 both as single header version

Validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions