D模板可用于的示例 [英] Examples of what D’s templates can be used for

查看:125
本文介绍了D模板可用于的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说D语言具有强大的元编程功能,可以在编译时执行功能.这听起来很令人兴奋,但是我发现很难想到没有它们很难完成的实际例子.

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 practical examples of things that are hard to accomplish without them.

有人可以举例说明D的元编程功能非常方便吗?

Can anyone give some examples of situations where D's metaprogramming features comes in very handy?

推荐答案

如果您想了解有关如何使用D的元编程工具的实际示例(CTFE或编译时函数求值,仅是其中之一,甚至不是最重要的)一个)看起来比D2标准库Phobos更好.大部分代码是由Andrei Alexandrescu编写的,他发明了C ++中的许多模板元编程技术,现在正与Walter Bright一起设计D的设计和实现.

If you want practical examples of how to use D's metaprogramming facilities (CTFE, or compile time function evaluation, is just one of these, and not even the most important one) look no further than Phobos, the D2 standard library. Much of this code is written by Andrei Alexandrescu, who invented a lot of the template metaprogramming techniques in C++, and is now working with Walter Bright on the design and implementation of D.

最好的模块是std.rangestd.algorithm.这些几乎完全由模板组成,由Andrei设计,并且鉴于它们使用的元编程量而令人惊讶地可读.我对这两个模块都做出了重要贡献,而阅读开始时在那里的代码基本上就是我的学习方式.

The best modules to look in are std.range and std.algorithm. These are almost entirely composed of templates, were designed by Andrei, and are surprisingly readable given the amount of metaprogramming they use. I've contributed significantly to both of these modules and reading the code that was there when I started was basically how I learned.

所有代码均已根据(极为宽松的)Boost许可进行许可,并且可以为

All of the code is licensed under the (extremely permissive) Boost license and can be viewed directly from your browser at the Phobos Trac site on dsource.org.

为了给您提供所看内容的路线图,D的元编程功能基本上分为4类:

To give you a roadmap of what you're looking at, D's metaprogramming facilities basically fall into 4 categories:

  1. 模板,它们基​​本上类似于C ++模板,但是具有一些附加功能,例如static ifstatic assert,可变参数模板和约束,它们基本上像概念,但更简单.

  1. Templates, which are basically like C++ templates, but with some added features like static if,static assert, variadic templates, and constraints, which are basically like concepts but simpler.

编译时间反射/自省.这包括内置的is()表达式和__traits,以及标准的库模块std.traits.

Compile time reflection/introspection. This includes the builtin is() expressions and __traits, as well as the standard library module std.traits.

Mixins .这些允许您采用模板(模板混合)或编译时字符串(字符串混合),并将其作为当前作用域中的代码进行评估.可以将字符串mixin看作是eval语句,只是字符串在编译时而不是在运行时被视为代码.

Mixins. These allow you to take either a template (template mixins) or a compile time string (string mixins) and evaluate it as code in the current scope. String mixins can be thought of as being kind of like an eval statement, except that the string is evaluated as code at compile time instead of at runtime.

编译时函数评估或 CTFE ,它允许在编译时评估满足某些条件的函数. CTFE的一个重要用途是,与字符串混合器结合使用,您可以在编译时将代码生成为字符串,然后在mixin语句出现的范围内将其评估为代码.有关示例,请参阅std.range.Lockstep和std.range.OutputRangeObject,我最近检查了Phobos的SVN版本.

Compile time function evaluation, or CTFE, which allows functions that meet certain criteria to be evaluated at compile time. One important use of CTFE is that, combined with string mixins, you can generate code as a string at compile time, and then evaluate it as code in the scope where the mixin statement occurs. For examples of this, see std.range.Lockstep and std.range.OutputRangeObject, which I recently checked into the SVN releases of Phobos.

这篇关于D模板可用于的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆