Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

in json11.cpp line 161 , why use static_cast, what if a json object is a runtime parsed object #118

Closed
678uhb opened this issue Apr 9, 2018 · 1 comment

Comments

@678uhb
Copy link

678uhb commented Apr 9, 2018

in json11.cpp line 161 , why use static_cast, what if a json object is a runtime parsed object

 // Comparisons
    bool equals(const JsonValue * other) const override {
        return m_value == static_cast<const Value<tag, T> *>(other)->m_value;
    }
    bool less(const JsonValue * other) const override {
        return m_value < static_cast<const Value<tag, T> *>(other)->m_value;
    }

@artwyman
Copy link
Contributor

artwyman commented Apr 9, 2018

The value in m_value may be parsed at runtime, but that doesn't matter here. This is dealing with JsonValue, which is a private template used as an internal implementation detail of Json, which supports only a known, fixed number of value types, identified by the type tag. Note that by the time we reach a call to this function (in operator== below) we've already checked the type tag and know the two values are of the same type. We always consider values of different types to be non-equal. This function only handles values of the same type, and thus simply uses static_cast to cast the argument to the correct sublcass and access m_value as the correct type.

@artwyman artwyman closed this as completed Apr 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants