Skip to content

Consider putting the user-defined literals in a namespace #1682

Closed
@ghost

Description

If I'm not mistaken, the user-defined literals are outside the nlohmann namespace. This means if the header is included twice, there will be a conflict. My use-case (or rather the library I'm contributing to) looks something like this:

  // Remove the include guards because third-parties may have included their own
  // version of nlohmann::json. 
  #undef NLOHMANN_JSON_HPP
  #undef NLOHMANN_JSON_FWD_HPP
  
  #define nlohmann library_internal_nlohmann_3_4_0
  #include <nlohmann/json.hpp>

The idea is that because the namespace is renamed to something that will not conflict, the library can be included multiple times. Previously, we would grab nlohmann_json as an external project and patch the user-defined literals out; however since we are moving to using find_package only, this is not possible (or would be ugly).

There is a downside to this: it would obviously break backwards compatibility. However, including it would be simple if it looked something like this:

namespace nlohmann {
inline namespace literals {
// ... define literals here
}
}

Then in user-code:

using namespace nlohmann::literals; // does not pollute global namespace

I am only 50/50 for this idea and would like to hear your thoughts.

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions