Tried Variadic Templates

Hi!

Yesterday I tried to use variadic templates with gcc for the first time of my life. The compiler support is definitely good, it works as expected with –std=gnu++0x (it is never bad to use this option, it will add support for the -header and the auto-keyword, which makes code much more readable). However, I want to tell you how I think about this new language feature: It is ugly. Well, you can implement tuples with it, they are more compile-time-efficient than typelists and have a short syntax, the three dots work magically at some locations, but: they are not really handable, you cannot pass multiple of those “parameter packs”, as far as I know you would have to convert them into typelists to perform operations like concatenation, sorting etc., you can use a parameter pack as type-specification for the parameters of the function, but there is no more general concept for functions with flexible parameter list, partial specialization is still forbidden for functions, there is no way to access them non-recursively, sorry they are for meta-programming, and why should meta-programming in C++ look like programming in Haskell but more complicated? Why could C++0x/C++1x not fix that? I do not like D, because they do not have proper support for value-semantics, implicit sharing etc. and there are many inconsistencies (many of them are inherited from C++), but they added useful extensions for Templates, making them really usable for meta-programming. Pretty simple task: Write a generic function taking 0..∞ strings, which will print them. You will see the drawbacks, but of course I would be happy about more readable code:

#include <iostream>
using namespace std;
 
// a struct, because there are no partial specializations
// for functions
template<typename... args>
struct Print
{
    // checking, because you cannot explicitly construct
    // a parameter pack of strings
    static_assert((sizeof...(args)) == 0, "illegal arguments");
    static void exec()
    {
    }
};
 
template<typename... args>
struct Print<string, args...>
{
    static void exec(string x, args... rest)
    {
        cout << x << endl;
        // recursion, because there is no static-for (ammendment: unlike the foreach for tuples in D)
        print(rest...);
    }
};
 
// that function works
// but it would be probably three lines in D
template<typename... T>
void print(T... args)
{
    Print<T...>
        ::exec(args...);
}

I think you see what I meant, variadic templates are nice for printf and tuples, but for any slidely different tasks they are still ugly.

Yes, I used it, really (skip deleted files and minor edits), it was ugly, but it provides some abstraction making the code – probably not readable – at least less redundant (I do not like copy&paste programming, the reason for copy&paste programming the lack of nice meta-programming features). Some last words why I am not using D: in C++ I can achieve anything using templates and implicit casts, and I have real value-semantics, D did not want to be like Java, but it forbids some stuff which is responsible for a lot of flexibility in C++. And if I would switch the language, I would try to choose a language involving some new, innovative and consistent concepts, that is not the case for D, maybe for Scala, but it does not allow value-semantics, too.

One Response to “Tried Variadic Templates”

  1. Templates Says:

    Their are lot of defects in this Variadic Templates.

    Seems to be spam, but I had not noticed it till today (Oct. 3rd 2011), thus I will keep it. ;) The User

Leave a Reply

XHTML: Use <blockquote cite="name"> for quotations, <pre lang="text    ∨ cpp-qt ∨ cpp ∨ bash ∨ other language"> for code, [latex] for formulas and <em> for em. Contact me if the comment does not get published, it may have accidentally been marked as spam.

Anti-Spam Quiz: