Examples of what D’s templates can be used for

后端 未结 4 410
长发绾君心
长发绾君心 2020-12-28 16:37

I hear that the D language has powerful metaprogramming features for executing functions at compile time. That sounds very exciting, but I find it difficult to think of prac

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-28 16:41

    I'm not well versed in the purposes of meta-programming. Here is my view on the three main constructs found in D.

    • Compile Time Function Evaluation is about performance. Making the compiler do as much work as possible so that less is done once the program is actually run.
      • Like building a cache of prime numbers, if you program makes use of them often.
    • Templates are about eliminating algorithm duplication. It isn't just generic programming, but since D has CTFE it isn't needed in the same cases as C++.
    • Mixins are about being able to generate code at compile time that is added to the program. It relies on things like templates and CTFE, but still an important piece not provided by the others.

提交回复
热议问题