I have some code in a template like so:
if constexpr ( std::is_same_v<T, CompletedGeneration> ) {
auto stat = stats->getGenerationStats();
} else if constexpr ( std::is_same_v<T, CompletedReset> ) {
auto stat = stats->getResetStats();
} else if constexpr ( std::is_same_v<T, CompletedRun> ) {
auto stat = stats->getRunStats();
} else {
static_assert( false, "Invalid type for helper function" );
}
The auto
for stat
was just to get it to compile temporarily. stats
is of type T
After this if-statement, there's a bunch of code which relies on stat
, so obviously I can't define it in the if
portion. I'm wondering, how would I go about defining it outside of the if
, since its type is dependent on the template parameter type T
(but is not a T
itself)?
Would I have to specify an additional template parameter U
, which takes in the type for stat
? Or have to use some sort of inheritance? Both of these options I'd rather avoid.