Tutorials and Introductions to C++ Expression Templates

断了今生、忘了曾经 提交于 2019-12-03 06:13:41

问题


What are good introductions to the creation of C++ expression template systems? I would like to express arithmetic on user defined types while avoiding temporary values (which may be large), and to learn how to do this directly rather than applying an existing library.

I have found Todd Veldhuizen's original paper and an example from the Josuttis C++ Templates book, and an article by Kreft & Langer. It is mentioned in Lecture 6 of a course on Modern C++, referring back to Josuttis.The POOMA library background introduces expression templates nicely.

I am looking for simple, clear expositions.


回答1:


You should get a copy of C++ Templates: The Complete Guide.

The code example to which you link doesn't have the accompanying text, which is quite helpful (the chapter on expression templates is 22 pages long). Without the text, all you have is code without any comments or explanation as to what it does and how and why it does it.




回答2:


I found Expression Templates Demystified to be fairly well explained. It starts off by showing how to implement mathematical expressions without templates and shows the basic building blocks and concepts.

The second part of the article shows how to refactor the initial implementation to use templates instead for improved performance and reduced code.

IMO, the article is a good alternative to the expression template chapter in 'C++ Templates: The Complete Guide'.




回答3:


I strongly encourage to look up Eric Niebler Boost.Proto library. A very fine introduction materials can be found over C++-next :

http://cpp-next.com/archive/2010/08/expressive-c-introduction/

and his talk at boost'con 2010 is also very interesting




回答4:


You've got all the sources except for the scientific c++ book, which is really just the original paper (wrt to this topic anyway) and is pretty out dated. You could look at C++ Template Metaprogramming for more modern techniques built from the expression templates ideas, but something "simple" is not going to be readily available until it IS simple.




回答5:


I suggest reviewing the Boost Operators at Boost Operators - Arithmetic. These are templated methods that extend fundamental arithmetic and comparison operations.




回答6:


for what you want to do, you should look at the code of Boost.ublas as it does exactly that.

Start with the file vector.hpp and then go back into the hierarchy (parents of vector<>). Matrices are a bit more complex but the mechanisn is the same.

Now I may advise not to try to reproduce the complexity of the library as a first try, like ublas_expression and things like that. You can do it simpler by making your type the base object and then implement a binary operator.

Look here too: http://www.bnikolic.co.uk/blog/cpp-expression-minimal.html



来源:https://stackoverflow.com/questions/2526862/tutorials-and-introductions-to-c-expression-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!