Skip to content

tag_invoke with non const ref to json value #1068

@mous16

Description

@mous16

In my code I would need to extract non-const pointers to underlying json values through functions such as if_int64 during JSON to C++ object conversion.
I see that all the if_ function implement a non-const overload, so I guess it's feasible.
My problem arises when i try to implement my custom tag_invoke.

The tag_invoke function for value_to usage has this form:

T tag_invoke( const value_to_tag< T >&, const value& );

I would need an overload where the value argument is passed as non-const reference, to be able to obtain non-const pointer to inner objects.
There is a specific reason why this overload is not present?

Delving into possible UB, I played with const_cast and I was unable to trigger any problem in code like the following:

class MyType {

    private:
    int64_t * my_inner_value;

    public:

    MyType(int64_t * my_inner_value) : my_inner_value{my_inner_value} {}

    const int64_t & get_my_inner_value() const { return *my_inner_value; }
    void set_my_inner_value(const int64_t & value) { *(this->my_inner_value) = value; }
};

MyType tag_invoke(json::value_to_tag<MyType>, const json::value & v)
{
    auto& o = const_cast<json::object&>(v.as_object());

    return { o.at(myKey).if_int64() };
}

But that smells like a possible bug in my program.
To my surprise, checking the code of non-const accessor for internal pointers, I saw that the same const cast approach is used.
Can I assume that my implementation will not trigger UB and will be reliable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions