-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
C++20 support by removing swap specialization #2176
C++20 support by removing swap specialization #2176
Conversation
I think this is slightly misguided, C++20 is making overloading std::swap UB because it's a bad thing to do in general. This change is not just about satisfying C++20 for the sake of it, it's an objective improvement regardless of the standard version. If moving away from std::swap to a proper ADL (which I am all for in general), then there is no particular reason to tie the transition to C++20. Users that are using the library today with C++11/14/17 still need to be notified somehow that the code they are in the process of writing is doomed. |
Overloading standard functions inside the Without ADL, the patch will make such code pick the default Although if everyone agrees, I will gladly remove the specialization entirely for all language version and promote the hidden friend instead. You are right that this is an improvement for any versions. |
I was more thinking along the lines of adding a deprecation warning so that we can ease the transition, while grandfathering in existing codebases. |
What would the deprecation say? Falling back to std::swap would probably work just fine right? |
@jaredgrubb exactly. No code written today will suddenly stop working. It's just that the default swap implementation will be picked instead, and might be slightly less efficient since it uses the move constructor instead of member-wise swap calls. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
🔖 Release itemThis issue/PR will be part of the next release of the library. This template helps preparing the release notes. Type
Description
|
[Describe your pull request here. Please read the text below the line, and make sure you follow the checklist.]
In C++20, the rule for customization point has changed.
From [namespace.std]/7:
This rule come from the paper P0551: Thou Shalt Not Specialize std Function Templates!
Swap is denoted as a customization point.
We can keep the specialization for older C++ version, but we should disable it for C++20 and above.
We also should provide a non-member swap function, ideally as a hidden friend.
This pull request implement those recommendations.
Please tell me if there's anything to fix or change in the patch.
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filesingle_include/nlohmann/json.hpp
. The whole process is described here.Please don't
#ifdef
s or other means.