I'm trying to write a generic wrapper for the C++ pugi xml library that can save and store values to/from xml.
They have implemented their xml node attributes' (stored as strings) access functions into functions such as attribute.as_int()
, attribute.as_bool()
etc.
I want to achieve the same functionality seen in the nlohmann::json library where you can call .get<T>()
on some json object and get some type.
The only way I can think of (which may not even work) is to use template specialization:
template <>
int foo<int>(xml_attribute param)
{
return param.as_int();
}
template <>
bool foo<bool>(xml_attribute param)
{
return param.as_bool();
}
And so forth.
Which seems to result in almost as much code as writing a non-generic wrapper...