I'm writing a C++ opensource project for configuration loading (from tree data structures like xml and json) based on boost::ptree
and boost::lexical_cast
.
To allow implicit conversion from a Tree
structure to any primitive or user defined types, I've
added a conversion operator template:
template<class Type>
operator Type() const {
return boost::lexical_cast<Type>(this->toString());
}
This works with types handled by boost::ptree. For all other types, the user can specialize this template:
namespace swiftree {
template<>
Tree::operator Vector3() const {
Vector3 ret;
ret.x = value<float>("x");
ret.y = value<float>("y");
ret.z = value<float>("z");
return ret;
}
}
So the user con do this:
Vector3 position = tree["position"];
Are there any drawbacks to using such technique? Are there better alternative ways to achieve the same?