为什么只有 C++(和 D 语言)提供可变参数模板功能?可变模板有那么好吗?
Why only C++(and D language) provides variadic templates feature? Are variadic templates that good?
为什么在许多编程语言中只有 C++(和 D 语言)支持可变参数模板?
为语言提供此功能是否有主要好处?
或者底层的复杂性和编译时间的增加(由于类型检查)使得它不值得付出努力?
我从哪里知道只有 C++(和 D 语言)支持可变参数模板?
Common Lisp 通过其宏系统提供比可变参数模板更好的功能(请注意,Lisp 宏比 C++ 模板或预处理器宏更强大)。 Lisp 比 C++(和 C)早几十年!尝试 SBCL(一个免费的优秀 Common Lisp 实现)。
顺便说一句,我认为 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.
许多(主要是学术)编程语言在 J.Pitrat 的博客上提供 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。
在带有 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 的现代操作系统(例如 Linux)上,将其编译为临时插件,并在相同的 运行.
中使用它
C++ 模板机制是 metaprogramming. Generating C or C++ code (wrongly called source code generation) and dlopen
ing 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 等的某种(不完整)形式)在 运行 时间生成代码。
直觉(关于 Common Lisp 宏的强大功能)是 Lisp 中的宏扩展可以进行 任意 计算(在 same Lisp 语言)在 compile 时,并且 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 可以很容易地(并且例行地)通过 Common Lisp 宏完成(当然那不是可能在 same 程序中的 C++ 中)。但是你需要读几本书才能掌握这个强大的想法,所以我不声称能够轻松解释它
C++ 越来越倾向于编译时编程功能,因为它服务于 C++ 所寻找的东西:性能。
使用可变参数模板和所有精彩的 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 库,它大量使用模板来映射到 LAPACK 的原始 C 函数,这非常难以使用且容易出错。 Armadillo 通过一个薄的 C++ 层使其变得超级简单、高性能和类型安全。
为什么在许多编程语言中只有 C++(和 D 语言)支持可变参数模板?
为语言提供此功能是否有主要好处?
或者底层的复杂性和编译时间的增加(由于类型检查)使得它不值得付出努力?
我从哪里知道只有 C++(和 D 语言)支持可变参数模板?
Common Lisp 通过其宏系统提供比可变参数模板更好的功能(请注意,Lisp 宏比 C++ 模板或预处理器宏更强大)。 Lisp 比 C++(和 C)早几十年!尝试 SBCL(一个免费的优秀 Common Lisp 实现)。
顺便说一句,我认为 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.
许多(主要是学术)编程语言在 J.Pitrat 的博客上提供 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。
在带有 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 的现代操作系统(例如 Linux)上,将其编译为临时插件,并在相同的 运行.
中使用它C++ 模板机制是 metaprogramming. Generating C or C++ code (wrongly called source code generation) and dlopen
ing 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 等的某种(不完整)形式)在 运行 时间生成代码。
直觉(关于 Common Lisp 宏的强大功能)是 Lisp 中的宏扩展可以进行 任意 计算(在 same Lisp 语言)在 compile 时,并且 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 可以很容易地(并且例行地)通过 Common Lisp 宏完成(当然那不是可能在 same 程序中的 C++ 中)。但是你需要读几本书才能掌握这个强大的想法,所以我不声称能够轻松解释它
C++ 越来越倾向于编译时编程功能,因为它服务于 C++ 所寻找的东西:性能。
使用可变参数模板和所有精彩的 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 库,它大量使用模板来映射到 LAPACK 的原始 C 函数,这非常难以使用且容易出错。 Armadillo 通过一个薄的 C++ 层使其变得超级简单、高性能和类型安全。