Tuples

This is a very short post on something I found while working on a generic Radix Sort.

In C++17, it is now possible to deduce class template arguments:

#include <tuple>
#include <type_traits>

using namespace std;

int main()
{
	tuple t(4, 3, 2.5); //class template arguments deduction
	static_assert(is_same_v<decltype(t), tuple<int, int, double>>);
}

This however does not (despite what I initially thought…) make std::make_tuple useless!

In generic code, I had wrote something like:

template<typename... Ts>
auto testFun1(Ts... v)
{
	return tuple{v...};
}

instead of

template<typename... Ts>
auto testFun2(Ts... v)
{
	return make_tuple(v...);
}

The actual piece of code was extracting the tail of a tuple: From a tuple t1, I wanted to get a tuple t2 = t1 with std::get<0>(t1) removed from it.

However:

testFun1(tuple{0.0, 1}) returns a tuple<double, int>.

while

testFun2(tuple{0.0, 1}) returns a tuple<tuple<double, int>>!


Moral of the story: extensive unit tests are nice!


Next posts will be on radix sort itself.

Links:

Class template argument deduction
std::is_same
radix sort

Written on July 11, 2017