-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Allow items() to be used with custom string #1765
Conversation
FWIW, looks good to me! Thanks for the tests too :) |
I dont' think the introduction of a custom https://stackoverflow.com/a/50555639 has an example. |
It's not exactly the same here. The example shown is about creating a to_string for other types. Here, I need a to_string that returns other type of string, the alt string. There's no easy way to select function on return value in c++. It tried something like this :
But it doesn't find the to_string for alt_string, as the iterator is in a detail namespace. If you have ideas to fix this, I'm open |
As @crazyjul says, ADL only kicks on argument types, not on return types as you suggest. Therefore the custom type has to be one of the args. I don't see any other way to do it than to make it |
@crazyjul @jaredgrubb Thanks for the answers. Yes indeed ADL does not help here. But still forcing the user to implement this function does not sound like the right approach. But I'm also running out of ideas how to do it better. |
There two other solutions :
There's other solutions requiring some templates, that would allow user to choose that default mode, but be able to override with a function of his own. I can make some proposition, but I need to experiment first |
What if you do:
That provides a default implementation that only requires that the custom string type is convertible from |
Done ;-) |
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.
Thanks! |
🔖 Release itemThis issue/PR will be part of the next release of the library. This template helps preparing the release notes. Type
Description
|
When using an alternative string, the iteration_proxy_value is not compatible, as it returns const std::string &. I replaced it by the type given string.
I made a function to abstract std::to_string, but I don't really like the way it's done and would like your opinion.
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.