I have situations where sometimes based on some bool
I want to call 2 constexpr
functions that return different types and assign it to auto
constant.
Unfortunately ternary operator needs types to be "similar".
I have workaround in the code below but it is quite verbose. Is there a better way?
#include <iostream>
#include <string>
constexpr int get_int(){
return 47;
}
constexpr std::string_view get_string(){
return "47";
}
constexpr bool use_str = false;
constexpr auto get_dispatch(){
if constexpr(use_str){
return get_string();
} else{
return get_int();
}
}
int main()
{
// what I want : constexpr auto val = use_str ? get_string():get_int();
// what works:
constexpr auto val = get_dispatch();
std::cout << val << std::endl;
}