C ++表达式模板 [英] C++ Expression Templates

查看:144
本文介绍了C ++表达式模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用C进行数值计算。我听说使用C ++表达式模板更好的科学计算。什么是简单的C ++表达式模板?


  1. 有没有书籍讨论使用C ++表达式模板的数值方法/计算?


  2. 以什么方式,C ++表达式模板比使用纯C更好?



解决方案


什么是简单的C ++表达式模板? b
$ b 是C ++模板元程序的一个类别,它延迟了子表达式的评估,直到完整的表达式被知道,从而可以应用优化(特别是消除临时表达式)。


有没有关于使用C ++表达式模板讨论数值方法/计算的书?


我相信ET是由Todd Veldhuizen发明的,他在15年前发表了一篇论文。 (看来,很多旧的链接到现在已经死了,但目前这里是它的一个版本。)关于它的一些材料是David Vandevoorde和Nicolai Josuttis的 C ++模板:完整指南


以什么方式,C ++表达式模板比使用纯C更好?


它们允许您以高效的方式编写代码,而不会失去性能。例如,

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

可以一直优化

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

哪个更快,但更难理解。


I currently use C for numerical computations. I've heard that using C++ Expression Templates is better for scientific computing. What are C++ Expression Templates in simple terms?

  1. Are there books around that discuss numerical methods/computations using C++ Expression Templates?

  2. In what way, C++ Expression Templates are better than using pure C?

解决方案

What are C++ Expression Templates in simple terms?

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.

Are there books around that discuss numerical methods/computations using C++ Expression Templates?

I believe ET's were invented by Todd Veldhuizen who published a paper on it 15 years ago. (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.

In what way, C++ Expression Templates are better than using pure 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.

这篇关于C ++表达式模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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