-
-
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
Functional visit #1480
Comments
Unfortunately, the |
I forgot about C++11, it's been a while. Examples include any time you don't really care about the type. For example when forwarding to another lib (in my case a mustache object creator). Or when you wish to use j.get([](auto&& n){
if constexpr (!std::is_same_v<std::string, std::decay_t<decltype(n)>>) {
// something
} else {
// something else
}
}); You can imagine a scenario where a templated function is specialized for certain types, and not others. In that case you really don't care about the de-serialized object type. j.get([](auto&& n){
my_templated_function(std::move(n));
}); Lets say you have "components" (as in entity-components) that are to be stored in a SoA manner. Your container : struct my_soa_component {
std::vector<int> ints;
std::vector<float> floats;
/* ... */
template <class T>
void push_back(T&& t) {
if constexpr(std::is_same_v<int, std::decay_t<T>>) {
// etc
}
}
}; Basically, this feature allows your library to interact in a much more natural way when dealing with templated code. |
It is just a convenience so we don't have to do all the ifs. |
It wouldn't be hard to implement (in C++11) such that your examples would work. In C++11, you could create a struct with templated-operator() (or with all the right non-template overloads) that is callable by all the JSON types. Then your example (using C++14 lambda-auto) would just naturally work. I would not call this function "get" as I don't think you could overload it to work well, maybe "visit" or something like that.
Bonus points if the visitor can return a value (that is returned by visit). And you could have |
If the passed in function could return values, then it would be a boon for interacting with However, I had forgotten c++11 didn't have templated lambdas. So, feel free to close the issue if you think that would introduce too much version dependent code. I was thinking of something rather simple though. |
I agree |
OK, let me summarize so I can check whether I understood everything. You would like to have a member function template<class ReturnType, class Visitor>
ReturnType visit(Visitor&& visitor) This
What to do in case of an array? I would assume I still need some help, and maybe some use cases to better grasp the problem we are trying to solve. |
(Looking at the examples in https://en.cppreference.com/w/cpp/utility/variant/visit, it seems this could also be a way to realize #1512) |
I was prototyping this in c++11, and I don't believe a deduced return type is possible? For an array, I would expect to receive an array type. So simply a edit : Whether you wish to make this a member function or global function is entirely up to you. I personally prefer a member function, but I don't believe this would change much and I'm fine with either. |
I still don't get the overall idea. I need more examples/use cases. And I need to understand whether this should be part of the library. |
No problem. |
Currently the user needs to deserialize values manually. For example :
Feature : Add an api that deserializes the value for the user, and calls a provided function with it:
The text was updated successfully, but these errors were encountered: