lundi 19 mars 2018

Some confussions about source code of Json11

When I read the source code of Json11,I find something that puzzled me.

    /* * * * * * * * * * * * * * * * * * * *
     * Static globals - static-init-safe
     */
    struct Statics {
        const std::shared_ptr<JsonValue> null = make_shared<JsonNull>();
        const std::shared_ptr<JsonValue> t = make_shared<JsonBoolean>(true);
        const std::shared_ptr<JsonValue> f = make_shared<JsonBoolean>(false);
        const string empty_string;
        const vector<Json> empty_vector;
        const map<string, Json> empty_map;
        Statics() {}
    };

    static const Statics & statics() {
        static const Statics s {};
        return s;
    }

/* * * * * * * * * * * * * * * * * * * *
 * Constructors
 */

Json::Json() noexcept                  : m_ptr(statics().null) {}
Json::Json(std::nullptr_t) noexcept    : m_ptr(statics().null) {}

Why the function statics return a reference value s that declared by static const?

Why use the function statics to get the member null?

Aucun commentaire:

Enregistrer un commentaire