I have a yaml-cpp which always converts into a std::string
, and sometimes also into something else. For example, if the string actually is "3.14"
, it would also convert into double
. I'd first like to try int
, then double
, then bool
, and if that doesn't work, convert to a std::string
. Alright, so let's nest those try
-catch
es:
try {
const int a = node.as<int>();
std::cout << "int!" << a << std::endl;
} catch (YAML::BadConversion) {
try {
const double a = node.as<double>();
std::cout << "double!" << a << std::endl;
} catch (YAML::BadConversion) {
try {
const bool a = node.as<bool>();
std::cout << "bool!" << a << std::endl;
} catch (YAML::BadConversion) {
const std::string a = node.as<std::string>();
std::cout << "string!" << a << std::endl;
}
}
}
Hm, the deeper and deeper nesting tells me that this isn't the best way to write that code.
Any suggestions on how to improve the design here? Flat nesting would certainly be advised.