c++header-filesforward-declarationnlohmann-json

What are good practice for forward declaration of nlohmann::json in C++?


I am working on a project in my spare time, and have been told to forward declare whenever possible. My project includes nlohmann::json across several files, so I thought to try and forward declare it for the headers.

At first, I tried a name-spaced forward declaration (like namespace nlohmann { struct json; };), but when I then include the library in my source files, the compiler complains:

"nlohmann::json" is ambiguousC/C++(266)

My second idea was to just make a JsonRef wrapper instead, and use a forward declaration of it, and only include the header for this JsonRef implemenation in source files.

It compiles, but I find this solution very unsatisfying. Is this stupid? Should I just get over myself and include it normally? I am just looking to learn and understand good programming practice.

The wrapper in question:

// json_fwd.hpp
#pragma once

#include "nlohmann/json.hpp"

// This header should only be included in source files.

struct JsonRef {
    JsonRef(const nlohmann::json& j_) : j(j_) {};
    operator const nlohmann::json&() const { return j; }
private:
    const nlohmann::json& j;
};

Solution

  • nlohmann already provides a json_fwd.hpp header with forward declarations, so all you need is:

    #include <nlohmann/json_fwd.hpp>
    

    If you look in that file, you see that class json is a specialization of the template basic_json, which is why your simple struct json; forward declaration didn't work.