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
#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.
March 24th, 2011 at 5:06 am
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