I have a function where I would like to pass a rapidjson::GenericObject as a parameter, e.g.
MyObject readObject(rapidjson::GenericObject<true/false, rapidjson::Value>& obj)
{
// ...
}
However, depending on how I extract the object, GenericObject is either const or not.
For example, it is
- When extracting from an array
rapidjson::GenericObject<true, rapidjson::Value> // true - When extracting from a doc
rapidjson::GenericObject<false, rapidjson::Value> // false
How can I have a function that can take both types of objects (true and false) so that I can then extract the members from it for further processing?
You can rewrite the
readObjectas a template function, which will deduce the class template arguments. There you can get the non-template boolean, which can be used inif constexpr, (since c++17) so that only the true branch will be retained at compile time.Something like as follows:
See a live demo in godbolt.org
Alternative to
if constexpr, the solution can be SFINAEd, if you do not have C++17 compiler support.