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
5
votes
1 answer

Dynamic dispatching of different SIMD implementations in header-only code. Possible at all?

I'm in the process of planing a vector math library. It should work based on expression templates to express chained math operations on vectors, like e.g. Vector b = foo (bar (a)); where a is a source vector bar returns an instance of an…
PluginPenguin
  • 1,576
  • 11
  • 25
5
votes
2 answers

Can algorithms be made compatible with expression templates?

Suppose I have some array-based code that is enabled to be used by expression templates. E.g., I have operator[] for these arrays overloaded and also have overloaded the arithmetic operator + etc. Now I would like to let the STL algorithm any_of…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
4
votes
2 answers

Arguments to a template function aren't doing any implicit conversion

For some strange reason, I can't get the template arguments in this one piece of code to implicitly cast to a compatible type. #include template struct vec; template <> struct vec { typedef…
4
votes
0 answers

On the life of a temporary object during expression templates

I am trying to understand an aspect of expression templates. Let's say we are applying the technique to matrices. Suppose we have three concrete matrices a,b,c of type Matrix>. Here the class Matrix is just…
myfirsttime1
  • 287
  • 2
  • 12
4
votes
1 answer

Expression templates not working for primitive type overloads under clang

I have a CRTP base class as follows: template class Base { public: // here is I think where the problem is inline const Derived& self() const {return *static_cast(this);} }; Then the derived…
Tasam Farkie
  • 319
  • 3
  • 14
4
votes
2 answers

Intermediate results using expression templates

in C++ Template Metaprogramming : Concepts, Tools, and Techniques from Boost and Beyond ... One drawback of expression templates is that they tend to encourage writing large, complicated expressions, because evaluation is only delayed until the…
PT.
  • 65
  • 5
4
votes
2 answers

Why doesn't std::basic_string support concatenation through expression templates?

Qt's QStrings can be concatenated by operator% which uses expression templates to precalculate the resulting string's size and optimize several chained calls to operator+. See this question of mine for more info. Why hasn't std::basic_string adapted…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
3
votes
3 answers

Why don't games use expression templates for math?

I can imagine expression templates doing awful things to compile times for things as pervasive as vectors/matrices/quaternions etc, but if it is such a great speed boost why don't games use it? It's quite obvious that SIMD instructions can exploit…
3
votes
1 answer

C++11: Preventing object being assigned to reference

Is there any way to create a type A such that: Given: A f(...); Then: Both auto&& a = f(...); and const auto& a = f(...); give a compile errors? The reason for this is that in this case, A is an expression template which contains references to…
Clinton
  • 22,361
  • 15
  • 67
  • 163
3
votes
0 answers

Why don't std::valarray get more attention from all C++ compilers? Expression templates mean big speedup to certain math-heavy tasks

Some codes like this (I'm not the author but I appreciate the work): // 22 cycles per pixel mandelbrot (cascadelake) #include #include #include #include #include #include #include…
3
votes
1 answer

Expression template implementation in Rust like in boost::yap

I am trying to teach myself Rust and as a challenging learning project I want to replicate the design pattern of the C++ expression template library boost::yap. I don't want a full fledged implementation, I just want a small demonstrator to find out…
joergbrech
  • 2,056
  • 1
  • 5
  • 17
3
votes
0 answers

Longest possible type name

Expression templates (for example) represent computations in a deeply nested class template. This can result in very long type names when the templates are instantiated. Is the length of a type name (or what's the correct term for it here?)…
florestan
  • 4,405
  • 2
  • 14
  • 28
3
votes
1 answer

Linearly Chained Factories and Move Semantics

I try to make chained factories. The chain would use move semantics to avoid constructing unnecessary objects. The chaining follows 3 rules: Unless a factory is flagged to make now, two factories can make an expression: A_maker ^ B_maker<...>…
R zu
  • 2,034
  • 12
  • 30
3
votes
2 answers

Why haven't modern compilers removed the need for expression templates?

The standard pitch for expression templates in C++ is that they increase efficiency by removing unnecessary temporary objects. Why can't C++ compilers already remove these unnecessary temporary objects? This is a question that I think I already…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
3
votes
0 answers

How to handle compound statements / side-effects within expression templates

While implementing expression templates for a vector/array type, I inevitably came to the point where I need to provide operator +=. Using the 'default' expression template scheme gives unintuitive results. The epxression a += b doesn't return the…
Pixelchemist
  • 24,090
  • 7
  • 47
  • 71