-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider putting the user-defined literals in a namespace #1682
Comments
I understand the issue, and #1539 could be part of the solution. However, removing the literal operator from the default public API would be a breaking change, right? |
Indeed, which probably means I don't expect this change until the next major release. |
Personally, I think putting UDLs into the global namespace (in a header) is a no-go anyway. Especially, if they have very generic names like |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I don't think this should be closed until a decision for or against is made. |
@MikeGitb I'm (random json.hpp user) in favour for putting the UDLs into their own namespace. fmt does that as well, see https://fmt.dev/latest/api.html?highlight=literal#literal-based-api. |
So am I |
How about the following changes:
So something like #ifndef JSON_LITERALS_SEPARATE
using namespace nlohmann::json::literals;
#endif // JSON_LITERALS_SEPARATE in the global namespace. Edited: Thanks @MikeGitb |
I think you have a typo or something like that in your snippet. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
#3590 doesn't fix this issue. I propose we follow the standard library approach to the letter: https://en.cppreference.com/w/cpp/chrono/operator%22%22h#Notes NLOHMANN_JSON_NAMESPACE_BEGIN
inline namespace literals {
inline namespace json_literals {
// define UDLs here
}
}
NLOHMANN_JSON_NAMESPACE_END Additionally, introduce the macro #if JSON_USE_GLOBAL_UDLS
using namespace nlohmann::literals::json_literals;
#endif Edit: Removed the unnecessary |
If you are already using inline namespaces, you don't need the |
You're right. I'll apply the change when I can. |
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:
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:
Then in user-code:
I am only 50/50 for this idea and would like to hear your thoughts.
The text was updated successfully, but these errors were encountered: