getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
990 stars 85 forks source link

undo change of parameter type in `NamedTuple::apply` #74

Closed grandseiken closed 6 months ago

grandseiken commented 6 months ago

58f1e41c830d2e598415e480939e38df3a76f785 changed the parameter type of the named tuple apply method from F&& to const F&, but this prevents it from being used with non-const objects. There is certainly no reason to disallow this (my fault for not adding a test).

liuzicheng1987 commented 6 months ago

Just to be clear...you want to pass an object to apply(...) that has an operator(...) which then has a side-effect on the object?

I am very much into functional programming and I personally think doing something like this is a code smell, but I guess you are right...there is no reason to disallow this. C++ is about freedom and letting people make their own design choices.

grandseiken commented 6 months ago

apply() returns void, so (at least in overload where the named tuple itself is const) the object passed must represent a side-effect of some sort, or else the call does nothing. Whether the side effect happens to apply to the object itself, or something else known to that object (e.g. a pointer captured in a lambda, or some global state) is immaterial.