Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why only C++(and D language) provides variadic templates feature? Are variadic templates that good? [closed]

Why is only C++ (and D language) out of many programming languages out there supporting variadic templates?

Is there a MAJOR BENEFIT of providing this feature to the language?

Or the underlying complexity and the compilation time increase (due to type checking) make it rather not worth the effort?

Where do I know that only C++ (and D language) supports variadic templates?

Wikipedia link: https://en.wikipedia.org/wiki/Variadic_template

like image 700
denis631 Avatar asked Nov 30 '22 08:11

denis631


2 Answers

C++ is moving more towards compile-time programming features because it serves what C++ is exactly looking for: Performance.

With variadic templates and all the wonderful type_traits features of C++ you can make very generic programs that will have almost zero impact on performance at run-time. An example is Armadillo library, where it uses templates very heavily to map to raw C functions of LAPACK, which is extremely difficult to use and error prone. Armadillo made it super-easy, performant and type-safe at the same time with a thin C++ layer.

like image 151
The Quantum Physicist Avatar answered Dec 05 '22 05:12

The Quantum Physicist


Common Lisp provides much better than variadic templates thru its macro system (beware, Lisp macros are much more powerful than C++ templates or macros for its preprocessor). Lisp predates C++ (and C) by decades! Try SBCL (a freely available excellent Common Lisp implementation).

BTW, I think that D and Terra and Template Haskell also have something similar to variadic templates, and probably other languages too .... I cannot explain why Lisp ideas vanished, the reasons are more sociological, educational and economical than technical; read also SICP, Practical Common Lisp and Lisp In Small Pieces.

Many (academic mostly) programming languages provide homoiconicity: code is data, and data is code - and is proof- (read about the Halting problem, Gödel incompleteness theorems, Curry-Howard correspondence, Rice's theorem, GEB book, Partial Evaluation, ...) . Read also about multi-stage programming (a type-safer form of metaprogramming). Read about CAIA on J.Pitrat's blog.

On modern operating systems (e.g. Linux) with dynamic loading of plugins (with dlopen and dlsym), it is possible (and sometimes interesting) to generate some C (or C++) code in some temporary file -i.e. to "compile to C" (or to generated C++) your DSL, compile that as a temporary plugin, and use it in the same run.

C++ template machinery is some (uncomplete) form of metaprogramming. Generating C or C++ code (wrongly called source code generation) and dlopening the corresponding plugin is another form of metaprogramming. And you could also use JIT compiling approaches (e.g. with libraries like libgccjit, LLVM, libjit, GNU lightning, etc...) to generate code at runtime.


The intuition (regarding the power of Common Lisp macros) is that a macro expansion in Lisp can do arbitrary computation (expressed in the same Lisp language) at compile time, and that LISP ASTs (called S-expressions-s) are easily processable in Lisp. Of course the "compilation" process in Lisp is very different than that of C++ (and most Common Lisp applications contain the compiler). Hence the equivalent of RPCGEN, of SWIG, of s11n, of Lemon can easily (and routinely) be done thru Common Lisp macros (of course that is not possible in C++ in the same program). But you need to read several books to grasp that powerful idea, so I don't claim to be able to explain it easily

like image 23
Basile Starynkevitch Avatar answered Dec 05 '22 04:12

Basile Starynkevitch