I need to do a typecheck on whether a variant type can hold a type at compile time.
I am converting an enum and a string to a variant, but I want the library to be compatible with a user provided variant (for the types they support). I would like to return nullopt if I can't create a valid variant.
template <typename CustomVariant>
std::optional<CustomVariant> AsCustomVariant(LargeEnum type, const std::string& name) {
case LargeEnum::ALPHA:
case LargeEnum::BETA:
return ConvertAlphaBeta(name);
case LargeEnum::GAMMA:
return ConvertGamma(name);
case LargeEnum::DELTA:
return ConvertDelta(name);
case LargeEnum::EPSILON:
return ConvertEpsilon(name);
default:
return std::nullopt;
}
The idea is to use some sort of template magic that can do something like:
if (std::type_can_convert<CustomVariant, Gamma>) {
return ConvertGamma(name);
} else {
return std::nullopt;
}
Aucun commentaire:
Enregistrer un commentaire