int run(std::string type, std::string interval)
{
if(type == "ST"){
if(interval == "SEC"){
constexpr unsigned int N = 10;
Runner<N> data();
data.parse();
} else{
constexpr unsigned int N = 20;
Runner<N> data();
data.parse();
}
}
else if(type == "JST"){
constexpr unsigned int N = 23;
Runner<N> data();
data.parse();
}
else{
constexpr unsigned int N = 5;
Runner<N> data();
data.parse();
}
}
I want to reduce the if statements and do the conditional checks on a separate function:
constexpr unsigned int arraysize(std::string type, std::string interval){
if(type == "ST"){
if(interval == "SEC"){
return 10;
} else{
return 20;
}
}
else if(type == "JST"){
return 23;
}
else{
return 5;
}
}
However this doesn't work because a constexpr function cannot have a parameter of nonliteral type std::string.
Is there a better way to take out conditional checks so that I end up with something like this:
int run(std::string type, std::string interval)
{
constexpr unsigned int N = arraysize(type, interval);
Runner<N> data();
data.parse();
}
arraysizeshould work in C++20 (or, since not all compilers support them yet, change the parameters tostd::string_view). Butrun()won't be able to call it like this, because function parameters are never considered to beconstexprinside of it.Runner<N> data();would be a function declaration... ->Runner<N> data{};