I find many nested std::conditional_t hard to read so I choose a different pattern(of calling decltype on function with auto return type):
template<bool is_signed, std::size_t has_sizeof>
auto find_int_type(){
static_assert(sizeof(int)==4);
if constexpr(is_signed){
if constexpr(has_sizeof==4){
return int{};
} else if constexpr (has_sizeof==8){
return std::int64_t{};
} else {
return;
}
} else {
if constexpr(has_sizeof==4){
return (unsigned int){};
}
else if constexpr (has_sizeof==8){
return std::uint64_t{};
} else {
return;
}
}
}
static_assert(std::is_same_v<int, decltype(find_int_type<true, 4>())>);
static_assert(std::is_same_v<unsigned int, decltype(find_int_type<false, 4>())>);
static_assert(std::is_same_v<void, decltype(find_int_type<false, 3>())>);
static_assert(std::is_same_v<void, decltype(find_int_type<false, 5>())>);
static_assert(std::is_same_v<std::int64_t, decltype(find_int_type<true, 8>())>);
static_assert(std::is_same_v<std::uint64_t, decltype(find_int_type<false, 8>())>);
static_assert(std::is_same_v<void, decltype(find_int_type<false, 9>())>);
My questions are:
Is there a better way?
Is this way slower to compile than std::conditional_t(assuming types that I need to instantiate are much more expansive than in this example where I just use built in types).
P.S. this is a toy example, IRCode I would be dealing with some more complicated types.