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

Boost.Proto : Is it possible for Proto transforms to evaluate a mixture expression of matrix & vector?

Now I am trying to teach g++ compiler linear algebra so that g++ can rewrite an expression such like (matrix * vector)(index) as the loop for evaluating the expression. Basically this is what I expect as a next article of the last article in the…
mito
  • 111
  • 8
3
votes
1 answer

How can I add parent references to Boost.Proto expressions?

I want to generate expression trees that have "reverse" references from child to parent. Is there a way to customize the Proto generator or domain so that the expression wrapper class (using proto::extends<>) contains a reference to the parent…
Aaron
  • 594
  • 2
  • 12
3
votes
1 answer

Segmentation fault in expression template using type created with auto keyword

I am constructing code with expression templates for computational kernels. My question is very short: Why does GNU G++ give a segfault (4.9.1, compiled with -O3) on the line containing the += in the following example: // Like this it crashes auto…
Chiel
  • 6,006
  • 2
  • 32
  • 57
3
votes
3 answers

Transform a parse tree of a polynomial to a parse tree of its evaluation according to Horner's scheme

Could you please point me to an algorithm that takes a (binary) parse tree for evaluating a polynomial expression in a single variable and returns an equivalent parse tree that evaluates the polynomial according to Horner's rule. The intended use…
2
votes
1 answer

How does boost::uBLAS handle nested products of matrices?

I read an article about the optimisation of nested product of matrices, using dynamic programming, and I wanted to see how it is implemented in boost::uBLAS. I'm not sure I understood the documentation (they talk about it at the very end of the…
pintoch
  • 2,293
  • 1
  • 18
  • 26
2
votes
0 answers

Automatic differentiation using expression templates c++

Introduction I am trying to learn about expression templates because it seems to be a very powerful technique for a wide range of calculations. I looked at different examples online (e.g. wikipedia), and I wrote a bunch of small programs that do…
2
votes
1 answer

How can I combine templated derived class in CRTP with derived class expression templates?

My goal is to implement a vector class Vec that allows for efficient computation of arithmetic expressions like auto vecRes = vecA + vecB * vecC. This is a known problem and a solution using the Curiously Recurring Template Pattern (CRTP) can be…
staerkHelmis
  • 77
  • 10
2
votes
0 answers

Combining Literals with Expression Templates Based On CRTP

This is the basic implementation I'm using for expression templates, based on the CRTP which allows me to conveniently combine multiple types of operations without also asking everything to be an expression tree. template struct…
Riddick
  • 319
  • 3
  • 15
2
votes
2 answers

How to use expression-templates for specific types?

When using expression templates, how do I create specializations? From the Wikipedia example, I can make a Vector sum template class like so: template class VecSum : public VecExpression > { E1 const&…
BadProgrammer99
  • 759
  • 1
  • 5
  • 13
2
votes
1 answer

expression templates - bad_alloc

i am currently working on a c++ project and now i am stuck already for a while. It's about delayed evaluation with expression templates and (for me at least) a strange bad_alloc. If you try the code below, you'll notice runtime error bad_alloc due…
user8705939
2
votes
0 answers

Expression templates for GPU

I have coded a vector expression template library for the CPU using template meta programming. However, I have difficulty creating GPU kernel for a given expression. Please advise on how I can create a string of the expression (c = a + b) given the…
2
votes
0 answers

Assigning to expression templates

I have little c++ experience, but now I need to look at some code that uses expression templates a lot, so I am reading chapter 18 of the book << C++ Templates: The Complete Guide >> and working on the example provided in the book. If you happened…
2
votes
2 answers

Expression template operator overloading problem with std::vector

I'm currently working on a numerical library that uses expression templates. Unfortunately I encountered a problem with my operator overloads. Consider the following stripped down example. #include namespace test { class test {}; …
ebo
  • 8,985
  • 3
  • 31
  • 37
2
votes
2 answers

How to prevent QStringBuilder from outliving the scope it was initialised in

I have been looking at changing some code to make use of QStringBuilder expression template for its purported performance improvements. Unfortunately this resulted in sections of my code starting to crash in places, an example of which…
sjdowling
  • 2,994
  • 2
  • 21
  • 31
2
votes
2 answers

Boost.Proto : How to make an expression terminal of a primitive array instead of std::vector?

Now I am trying to make yet another mini-EDSL (embedded domain-specific language) for vector expressions. Actually Boost.Proto users' guide already provided such an EDSL example, "Lazy Vector", where vector expressions are made of std::vector.…