Skip to main content

Templates and how to fold them

Variadic templates appeared in C++11 to cover such cases when you would have a template functions that could have a numerous members of different types. Doesn't it remind you of variadic functions that uses va_start, va_arg, va_end and so others? Cause it should be.



What would you like to do - write a bunch of specified functions for each type in order to cover all cases of it's usage. Many of pre 11 standard projects that have to deal with basic types provides such interfaces that provides you with overloaded versions of single function. In a UI library I'm using on my current project there is a logging class that provides a string formatting function that has 31 overloaded versions. Is there such a case where someone would need to pass 32 parameters into it? I don't even want to know.

The most popular example of variadic templates I can imagine is a std::tuple about which I've previously wrote a post.

Let's get back to the topic. Templates are used widely across STL and boost libraries. I'm pretty sure the maintainers of some features and libraries has applied variadic templates into their projects pretty already.

Here we have a couple functions that do a lot of work being relatively simple and self-explanatory at the same time.

tprintf is a variation of printf family functions but with a one major difference. Using the function you don't need to specify types of variables you would like to pass into printing function. By the way this printing function is really slow because of cout usage in general. cout has to deduce all the types of passed parameters when printf could just run predefined allocator for a variable that has been defined in a format string. Let's say complexity it that way - complexity of cout is way higher which means that it takes more time to run defined version of operator<< for each variable, convert it into string and put it into a output cache before flushing it. By the way I've spent so much of your time cause this example I've got from cppreference.com while I was struggling with installing gcc-7 on my a bit old version of kubuntu.

So, there is another sample function add which takes numerous variables and add them together into big and nice variable. Basically it depends on variables and their operator+ that's why I choose string. The code is pretty simple but there is a couple issues you can face while trying it by yourself. Folding part of typename has to be at the end of template definition list, other way it won't work.

I was trying to write a bit more complex example and got into SFINAE and it's magic, but I failed miserably and decided to write a post on SFINAE exclusively a bit later. So stay tuned!

Popular posts from this blog

Tuples: are they useful?

In some languages there is such a thing as tuple which makes real to write such code (this time it's in python):


def f(x):
 # do stuff return (True, modified_string)

success, modified_string = f(something) 
C++ hadn't such feature until C++11. That was one of the reasons to pass variable as referenced argument, which ended up creating huge code bases that have unused arguments in it's functions. That happened in order to save revers compatibility and allow clients to use stable interfaces to access libraries without checking the version of dynamically attached library in order to use correct functions.

Maturing, relocation, getting to the point

So it's been a long time I've wrote something for the blog.
Since the last post few things have changed. For example - I've moved in a foreign country. I went into Ukraine which is not a big deal you would say, but for me it's a pretty significant change in my life.



I've worked a two years in solo on a video related projects, one of which was related to trans-coding video on fly using a gstreamer and the other one was about storing and providing access to a video content by special tags, this one was a nightmare.

The gstreamer is a neat open source solution in video processing libraries. The people designed it are crazy ones - such a over-complicated implementation to achieve flexibility and effectiveness in processing video on various platforms and hardware. The most important about this library for me was it's non-existing support. Or should I say community driven support? I don't know even for now. At the time I've had a couple questions, so basical…