Skip to content

template-based as<type>() accessor #1066

Closed
@JulianvDoorn

Description

@JulianvDoorn

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
It's slightly disappointed when I have to write a helper function which allows to determine the type of the lookup at compile time.
Describe the solution you'd like
A clear and concise description of what you want to happen.
I wish to be able to call JsonValue::as() as an alternative of JsonValue::asDouble().

Describe alternatives you've considered
I've written a function that uses metaprogramming features to determine the type which is being looked up at compile time. (assumes C++17, but I imagine an implementation is possible, SFINAE?):

template <class dat_type>
dat_type jsonValueAsType(const Json::Value& jsonVal) {
    if constexpr (std::is_same_v<dat_type, double>) {
        return jsonVal.asDouble();
    } else if constexpr (std::is_same_v<dat_type, float>) {
        return jsonVal.asFloat();
    }
}

Additional context
No additional context is required, I believe. But with focus of the C++ community to work towards better metaprogramming facilities, I think it is in place to add an JsonValue::as<T>() function.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions