I am trying to write a function like this
template<
bool b, RT = std::conditional_t<b,
std::tuple<int, int, int, int>,
std::tuple<int, int, int, int, double, double, double, double>
>
RT function()
{
int i1, i2, i3, i4;
if constexpr(b)
{
double i5, i6, i7, i8;
return { i1, i2, i3, i4, i5, i6, i7, i8 };
}
else
{
return { i1, i2, i3, i4 };
}
}
Is there a way to create a templated typedef for the tuple so that I can simplify the above function
template<typename T, int N>
using tuple_t = std::tuple<T, T, ... N1 times>
template<typename T1, int N1, typename T2, int N2>
using tuple_t = std::tuple<T1, T1, ... N1 times, T2, T2, ... N2 times>
You can use return type deduction and replace the aggregate initialization with a call to make_tuple
:
template<bool b>
auto function()
{
int i1, i2, i3, i4;
if constexpr(b)
{
double i5, i6, i7, i8;
return std::make_tuple(i1, i2, i3, i4, i5, i6, i7, i8);
}
else
{
return std::make_tuple(i1, i2, i3, i4);
}
}
If you still need the return type you can simply make a trait:
template <bool b>
using return_t = decltype(function<b>());