Skip to content

get<T> for types that are not default constructible #996

@bradymadams

Description

@bradymadams

Currently the get<T> method requires that T is default constructible. Might it make sense to overload get so that it can take variadic arguments and construct the new object with a non-default constructor? For example - below is a minor variation of get. Any reason why this is a bad idea?

template<typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
         detail::enable_if_t <
             not std::is_same<basic_json_t, ValueType>::value and
             detail::has_from_json<basic_json_t, ValueType>::value and
             not detail::has_non_default_from_json<basic_json_t, ValueType>::value,
             int> = 0, typename ...Types>
ValueType get(Types... args) const noexcept(noexcept(
                                   JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
{
    // we cannot static_assert on ValueTypeCV being non-const, because
    // there is support for get<const basic_json_t>(), which is why we
    // still need the uncvref
    static_assert(not std::is_reference<ValueTypeCV>::value,
                  "get() cannot be used with reference types, you might want to use get_ref()");

    ValueType ret(args...); // Allows non-default constructors to be used
    JSONSerializer<ValueType>::from_json(*this, ret);
    return ret;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions