Hi!

#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

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: