Skip to content

Bug: returning reference to local temporary object #2064

@nlohmann

Description

@nlohmann

Clang reports the following warning:

../single_include/nlohmann/json.hpp:18265:20: error: returning reference to local temporary object [-Werror,-Wreturn-stack-address]
            return *ptr;
                   ^~~~

Context:

template<typename ReferenceType, typename ThisType>
static ReferenceType get_ref_impl(ThisType& obj)
{
    // delegate the call to get_ptr<>()
    auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();

    if (JSON_HEDLEY_LIKELY(ptr != nullptr))
    {
        return *ptr;          // <----- warning
    }

    JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name())));
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions