Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Add support for deserialization of STL containers of non-default constructable types (fixes #2574). #2576
Add support for deserialization of STL containers of non-default constructable types (fixes #2574). #2576
Changes from 1 commit
1e825e4
c0a8b45
1b113f7
23f462b
672e8bf
6ebf274
6278f31
6ef1614
fc8c584
fbf6df6
d7c0f15
6eb37e9
848927a
130382f
333612c
322bc99
8e79917
2b86513
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
I prefer to keep a single
from_json
overload in these cases, and forward tofrom_json_pair_impl
, which use apriority_tag
to ease the SFINAE pain, and to make it possible to addfrom_json_pair_impl
overloads in the future.Also, removing the SFINAE for overloads with
detail::tag
will allow default-constructible types to be passed as well, thus reducing code duplication (the first overload can call the second now, instead of copy-pasting)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.
What would the signature of this
from_json
function be? The second argument could be eitherstd::pair<A1, A2>
oridentity_tag<std::pair<A1, A2>>
, and I'm not seeing how to automatically deduceA1
andA2
when the second argument is e.g.PairRelatedType
.Or do you mean to have a single overload taking an
std::pair
and one takingidentity_tag<std::pair>
and then both of those dispatching tofrom_json_pair_impl
? In that case, if the SFINAE is removed for thedetail::tag
overloads, then there will be two possible overloads available foradl_serializer
to call (in case of default constructable types), so then that won't compile anymore due to ambiguity.I'm probably just overlooking something...
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.
I mean something like this: https://github.com/theodelrieu/mgs/blob/master/codecs/include/mgs/codecs/basic_codec.hpp#L109
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.
I've pushed an update which removes some code duplication and uses
priority_tag
to dispatch to the preferred function. Is this what you had in mind?Note that
adl_serializer
now potentially has twofrom_json
functions. This is e.g. the case for all default-constructablestd::array
,std::pair
, andstd::tuple
. This doesn't cause ambiguity becausejson::get()
uses SFINAE to select the "object returning"from_json()
in that case (discardedget()
vs chosenget()
). This choice seems pretty deliberate, and looking at the code it's a logical choice due to possible bypassing at least 1 move construction (at least pre-C++17, although with optimizations there's probably no difference).While doing some testing I did notice that the "chosen"
get()
gets called when deserializing astd::vector<float>
. I found that quite surprising. I get how thisadl_serializer::from_json()
could now exist due to my code changes, but looking at the existing code it's not clear to me whichdetail::from_json
would get called in that case. Just something I found odd which I figured was worth mentioning.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.
Yes, you can remove this line for the
priority_tag<0>
overload though:enable_if_t < !std::is_default_constructible<std::array<T, N>>::value, int > = 0 >
This is the point of
priority_tag<0>
, it forces priority inside the overload set so you can just keep theis_default_constructible
check forstd::pair<A1, A2>
.IIRC (it's been a while since I've looked at the code), there is support for such overloads for
adl_serializer
specializations. I don't see why there would not be one for the base template indeed.Seems like a change of behavior. To be honest, the template code is quite dusty, I've learned quite a lot since I wrote it, and it's quite hard to figure out all that's going on inside...
For instance, the different
get()
overloads would be a nice case forpriority_tag
. All the rules could be refined using emulated concepts.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.
I've updated some more code and fixed a potential issue with my
std::pair
deserialization. Is this OK?