-
Notifications
You must be signed in to change notification settings - Fork 105
Description
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?