Variadic Template in C++: Implementing Unsophisticated Tuple

Variadic Template in C++: Implementing Unsophisticated Tuple

hackernoon.com hackernoon.com2 weeks ago in #Dev Love56

@IndianWestCoastVishal Chovatiya Software Developer⌨, Fitness Freak🏋, Geek🤓, Hipsterđź•´, Blogger👨‍💻, Productivity Hacker⌚ From C 11, std::tuple is an incredible expansion to Modern C , that offers a fixed-size col­lec­tion of het­ero­ge­neous values. Un­for­tu­nately, tu­ples can be somewhat dubious to manage in a conventional fash­ion. But, subsequently released C stan­dard in­tro­duced a few fea­tures & helpers that greatly re­duce the nec­es­sary boil­er­plate. So, in this article, I will explain the variadic template in C with the help of unsophisticated tuple implementation. And also walks you through a tricky part of tuple i.e. loop through tuple element. In spite of the fact that I have shrouded the variadic template in my prior article i.e. C Template: A Quick UpToDate Look. So, my focus here would be a blend of variadic template & tuple implementation with more up to date C gauges. Motivation It is often useful to define class/struct/union/function that accepts a variable number and type of arguments. If you have already used C you’ll know that printf function can accept any number of arguments. Such functions are entirely implemented through macros or ellipses operator. And because of that it has several disadvantages like type-safety, cannot accept references as arguments, etc. Variadic Class Template: Implementing Tuple Class So, let’s build our own ADT same as std::tuple with the help of variadic template. The variadic template in C usually starts with the general (empty) definition, that also serves as the base-case for template recursion termination in the later specialisation: template struct Tuple { }; This already allows us to define an empty structure i.e. Tuple object;, albeit that isn’t very useful yet. Next comes the recursive case specialisation: template< typename T, typename... Rest // Template parameter pack > struct Tuple { // Class parameter pack T first; Tuple rest; // Parameter pack expansion Tuple(const T& f, const Rest& … r) : first(f) , rest(r…) { } }; Tuple t1(false); // Case 1 Tuple t2(1, ‘a’, “ABC”); // Case 2 How Does Variadic Class Template Works? To understand variadic class template, consider use case 2 above i.e. Tuple t2(1, ‘a’, “ABC”); The declaration first matches against the specialization, yielding a structure with int first; and Tuple rest; data members. The rest definition again matches with specialization, yielding a structure with char first; and Tuple rest; data members. The rest definition again matches this specialization, creating its own string first; and Tuple rest; members. Finally, this last rest matches against the base-case definition, producing an empty structure. You can visualize this as follows: Tuple -int first -Tuple rest -char first -Tuple rest -string first -Tuple rest -(empty) Variadic Function Template: Implementing get() Function for Tuple Class So, far we have designed data structure with variable number and type of data members. But still, it isn’t useful as there is no mechanism to retrieve data from our Tuple class. So let’s design one: template< size_t idx, template class Tuple, typename... Args > auto get(Tuple &t) { return GetHelper::get(t); } As you can see this get function is templatized on the idx. So usage can be like get(t), similar to std::tuple. Though, the actual work is done by a static function in a helper class i.e. GetHelper. Note also the use of a C 14-style auto return type that makes our lives significantly simpler as otherwise,  » Read More

Like to keep reading?

This article first appeared on hackernoon.com. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply