简体   繁体   English

C ++表达式模板

[英]C++ Expression Templates

I currently use C for numerical computations. 我目前使用C进行数值计算。 I've heard that using C++ Expression Templates is better for scientific computing. 我听说使用C ++ Expression Templates更适合科学计算。 What are C++ Expression Templates in simple terms? 简单来说,什么是C ++表达式模板?

  1. Are there books around that discuss numerical methods/computations using C++ Expression Templates? 是否有书籍使用C ++表达模板讨论数值方法/计算?

  2. In what way, C++ Expression Templates are better than using pure C? 以什么方式,C ++表达模板比使用纯C更好?

What are C++ Expression Templates in simple terms? 简单来说,什么是C ++表达式模板?

Expression templates are a category of C++ template meta programming which delays evaluation of subexpressions until the full expression is known, so that optimizations (especially the elimination of temporaries) can be applied. 表达式模板是C ++模板元编程的一种类型,它延迟子表达式的评估,直到已知完整表达式,从而可以应用优化(尤其是消除临时值)。

Are there books around that discuss numerical methods/computations using C++ Expression Templates? 是否有书籍使用C ++表达模板讨论数值方法/计算?

I believe ET's were invented by Todd Veldhuizen who published a paper on it 15 years ago. 我相信ET是由Todd Veldhuizen发明的,他在15年前发表了一篇论文。 (It seems that many older links to it are dead by now, but currently here is a version of it.) Some material about it is in David Vandevoorde's and Nicolai Josuttis' C++ Templates: The Complete Guide . (似乎许多旧的链接到现在已经死了,但目前这里有一个版本。)有关它的一些材料是David Vandevoorde和Nicolai Josuttis的C ++模板:完整指南

In what way, C++ Expression Templates are better than using pure C? 以什么方式,C ++表达模板比使用纯C更好?

They allow you to write your code in an expressive high level way without losing performance. 它们允许您以富有表现力的高级方式编写代码而不会降低性能。 For example, 例如,

void f(const my_array<double> a1, const my_array<double> a2) 
{ 
  my_array<double> a3 = 1.2 * a1 + a1 * a2; 
  // ..
}

can be optimized all the way down to 可以一直优化到

for( my_array<double>::size_type idx=0; idx<a1.size(); ++idx ) 
  a3[idx] = 1.2*a1[idx] + a1[idx]*a2[idx]; 

which is faster, but harder to understand. 这更快,但更难理解。

Adding to sbi's answer, expression templates implement high-level peephole optimizations using templates for pattern matching and synthesis. 除了sbi的答案之外,表达式模板还使用模板进行模式匹配和合成来实现高级窥孔优化

They also add syntactic sugar , or make your code more readable, by allowing you to specify the algorithm in terms of simple operations. 它们还允许您根据简单操作指定算法,从而添加语法糖 ,或使您的代码更具可读性。 So, in this case, simplicity and elegance are achieved through optimization by metaprogramming. 因此,在这种情况下,通过元编程优化可以实现简单和优雅。 At least, if you do everything right. 至少,如果你做的一切都正确。

There is a nice article on C++ template math in the good old Flipcode archive (sure brings back memories): 在旧的Flipcode存档中有一篇关于C ++模板数学的好文章(肯定会带来回忆):

http://www.flipcode.com/archives/Faster_Vector_Math_Using_Templates.shtml http://www.flipcode.com/archives/Faster_Vector_Math_Using_Templates.shtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM