Questions tagged [expression-templates]

Expression templates is a C++ template metaprogramming technique in which templates are used to represent part of an expression as a compile-time structure representing a flattened abstract syntax tree of said expression. It enables idioms like lazy evaluation and inter-procedurale optimization within the language itself.

Expression templates is a C++ template metaprogramming technique in which templates are used to represent part of an expression as a compile-time structure representing a flattened abstract syntax tree of said expression. It enables idioms like lazy evaluation and inter-procedural optimization within the language itself.

Seminal works on Expression Templates go back to Vandevoorde et al in their 2002 book 'C++ Templates: The Complete Guide' and Veldhuizen in the 1995 paper called 'Expression Templates'.

Classically, Expression Templates are used to capture arbitrary large expressions without any creation of temporaries. Said expression are usually transformed and turned into actual code within the proper evaluation context. Linear algebra libraries are the most frequent example of this technique but other domains like parser generator (Boost.spirit) or State Machine description (Boost.MSM) are known.

If many people still write Expression Templates by hand, Boost.proto provide a framework to develop them without all the required boilerplate. It also provides tree manipulation functions that simplify transformation of said expression.

Expression Template is often tied to Domain Specific Language as they provide an efficient way to implement hem inside C++.

110 questions
35
votes
2 answers

Expression templates and C++11

Let's look at one particular benefit of expression templates: ETs can be used to avoid vector-sized temporaries in memory which occur in overloaded operators like: template std::vector operator+(const std::vector& a, const…
ritter
  • 7,447
  • 7
  • 51
  • 84
20
votes
5 answers

Providing different implementations of a class depending on lvalue/rvalue when using expression templates

The problem Suppose we implement a string class which represents, uhm, strings. We then want to add an operator+ which concatenates two strings, and decide to implement that via expression templates to avoid multiple allocations when doing str1 +…
peppe
  • 21,934
  • 4
  • 55
  • 70
18
votes
4 answers

How to integrate a library that uses expression templates?

I would like to use the Eigen matrix library as the linear algebra engine in my program. Eigen uses expression templates to implement lazy evaluation and to simplify loops and calculations. For example: #include int main() { int size…
Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146
14
votes
1 answer

Why is Overload Resolution favoring unconstrained template function over a more specific one?

I have this minimal expression template library with a multiplication, i.e. template struct mul { const T &v1; const U &v2; }; template mul operator*(const T &one, const U &two) { …
Nibor
  • 1,236
  • 9
  • 23
14
votes
1 answer

Expression templates are not being inlined fully

I have the first version of a math library completed, and for the next step I'd like to turn to expression templates to improve the performance of the code. However, my initial results are different than I expected. I am compiling in MSVC 2010, in…
Corey
  • 3,125
  • 1
  • 18
  • 14
13
votes
4 answers

Check for multiple values when using comparison operators

I've always been under the impression that for any comparison statement, i.e. X == Y or X != Y is the format, and you chain statements together with && or ||. Is there not some way to write X == (Y || Z) instead of X == Y || X == Z? Edit: Since it…
Drise
  • 4,310
  • 5
  • 41
  • 66
12
votes
1 answer

Expression templates vs. hand-written code

I am currently writing a C++ template expression library and comparing some instantiations with hand-written code at assembly level. The hand-written function is the following: spinor multiply(vector const& a, vector const& b) { spinor…
cschwan
  • 3,283
  • 3
  • 22
  • 32
10
votes
2 answers

Matching a Boost.Proto grammar to a type

I'm trying to make a grammar in Boost.Proto that matches a vector type, but when I give it a terminal of that type, it doesn't match the grammar. The type definition looks like this: template struct vector { typedef T…
vedosity
  • 720
  • 4
  • 15
10
votes
6 answers

Tutorials and Introductions to C++ Expression Templates

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…
grrussel
  • 7,209
  • 8
  • 51
  • 71
9
votes
2 answers

Prevent expression templates binding to rvalue references

I understand that doing something like the following: auto&& x = Matrix1() + Matrix2() + Matrix3(); std::cout << x(2,3) << std::endl; Will cause a silent runtime error if the matrix operations use expression templates (such as boost::ublas). Is…
Clinton
  • 22,361
  • 15
  • 67
  • 163
9
votes
2 answers

Expression templates and ranged based for in C++11

It is my understanding that expression templates will break on ranged based for in C++11, as for (auto x : expr) has an implicit auto&& __range = expr in it, and this will result in dangling references. Is there a way create expression template…
Clinton
  • 22,361
  • 15
  • 67
  • 163
8
votes
2 answers

Can capture-by-reference in expression templates coexist with type deduction?

Expression templates are often used as an optimization technique to avoid the creation of temporary objects. They defer constructing the complete object until the template is used in an assignment or initialization. This finds use in string…
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
7
votes
1 answer

How to write a third-party library wrapper class around expression templates

We are trying to implement a new C++ code in my research group to perform large numerical simulations (finite elements, finite difference methods, topology optimization, etc.) The software will be used by people from academia and industry alike. For…
6
votes
3 answers

Transforming an expression template tree

Given an expression template tree, I want to create a new optimized tree before processing it. Consider the following example of a multiplication operation: a * b * c * d, which produces, due to the left-to-right associativity of operator*, the…
A.L.
  • 1,133
  • 1
  • 12
  • 19
5
votes
1 answer

How to actually use Expression Template

In the wikipedia article, it provides some template classes. I want to use it in actual code. How can I do that? I found there is almost no way for me to instantiate a Vec object.
user1096734
1
2 3 4 5 6 7 8