C++ Expression Templates

前端 未结 3 1047
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 00:01

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

3条回答
  •  一整个雨季
    2020-12-09 01:03

    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 a1, const my_array a2) 
    { 
      my_array a3 = 1.2 * a1 + a1 * a2; 
      // ..
    }
    

    can be optimized all the way down to

    for( my_array::size_type idx=0; idx

    which is faster, but harder to understand.

提交回复
热议问题