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
0
votes
0 answers

Operator overloading in matrix using expression templates

I have been trying to figure out expression templates since the last couple of days but haven't been able to get past this. I am building a matrix starting with the add operator. I am building using c++14. My matrix.h looks like this: template…
Sarthak Agarwal
  • 128
  • 1
  • 2
  • 16
0
votes
0 answers

Expression template code not optimized fully

I have the following linear algebra function call (vector-vector addition) in C++. int m = 4; blasfeo_dvec one, two, three; blasfeo_allocate_dvec(m, &one); blasfeo_allocate_dvec(m, &two); blasfeo_allocate_dvec(m, &three); // initialize vectors…
Nibor
  • 1,236
  • 9
  • 23
0
votes
1 answer

Implementing an assignment operator for a row class in a matrix library which uses expression templates

Suppose we have a matrix class which makes use of expression templates such that proxy objects are used to make it possible for the compiler to optimize compound expressions. Now, it's quite natural to create a row class of the following…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
0
votes
1 answer

Passing generic class type in java

I'm trying to see if the template expression pattern can be imitated in Java, to do optimizations like loop fusion. As an example, I port the c++ classes found in this expression template example to java classes:…
AaronF
  • 2,841
  • 3
  • 22
  • 32
0
votes
2 answers

Are the expression templates technique used in Dlib still choice given move semantics in C++11?

With the advent of move semantics I am wondering if this specific template technique implemented by King in his dlib library are still useful after temporarily created objects are able to pass on ownership with the help of move semantics or am I…
CD86
  • 979
  • 10
  • 27
0
votes
1 answer

How template expressions get rid of temporaries

I was reading some articles and in many of them mentioned about expression templates can avoid using temporary objects. But none of them mentioned how this is done. As far as I know, due to the design architecture operations are done using temporary…
user1670773
0
votes
1 answer

Expression Template performance for deep nested expressions

I am investigating expression template performance in matrix multiplication. I am unrolling the loop in the matrix multiplication, and I find that the performance of native doubles is equal to that of the expression templates until the depth of the…
0
votes
0 answers

common_type compile error with certain compile options

Context: I am playing around with writing expression templates and C++11 features. The attached code sample is just an experament for fun. In this variation of ETs, each expression keeps track of its own return type. common_type is then used by the…
0
votes
2 answers

template method matching derived type instead of base

I have a set of operators that I need to override for expression templating. I would like all derived classes of a base type match to the base type. Other things would then be caught by a generic type. Unfortunately, the generic type grabs the…
Cory
  • 151
  • 12
0
votes
1 answer

Assignment to an array subsection: am I assigning to an Rvalue here, and if so how do I fix it?

In the hope of making my Fortran code easier to port over to C++ one day, I've been working on some expression template code to provide whole-array arithmetic operators and the ability to copy from and assign to specified sections of longer arrays.…
Eos Pengwern
  • 1,467
  • 3
  • 20
  • 37
0
votes
1 answer

how to evaluate expression while declaring variable (in expression templates)

I'm trying to explore expression templates in C++. I'm trying to create a class for a 3D vector (basically vector of size 3) for storing coordinates, space vectors, forces, etc. which basically have three components. So far I have only implemented…
Pranav
  • 560
  • 1
  • 8
  • 21
0
votes
1 answer

Expression Templates: error C2784 'could not deduce template argument'

I am working on a vector class that uses expression templates. I have the following (reduced) code. In "Vector.h": #include #include #include #include #define NULLVAL(a) (*((a*) 0)) namespace…
LRC
  • 1
  • 2
0
votes
3 answers

Befuddling Expression Template Segfault with O3

On my gcc-4.8.1, I've compiled the following program with two commands: g++ -Wfatal-errors -std=c++11 -Wall -Werror test.cpp -o test -g g++ -Wfatal-errors -std=c++11 -Wall -Werror test.cpp -o test -O3 -g The first executable has the expected…
Suedocode
  • 2,504
  • 3
  • 23
  • 41
0
votes
1 answer

Profiling Expression Template

I'm trying to profile the expression template similar to the one on the book "C++ Template" by David Vandevoorde. Below is my theoretical analysis, which is probably wrong because the test shows unexpected results. Suppose the test is about: R = A +…
user3156285
  • 353
  • 1
  • 3
  • 15
0
votes
0 answers

C++: Performance of Loop in Expression Template

I'm using Expression Templates in a vector-like class for transformations such as moving averages. Here, different to standard arithmetic operations, the operator[](size_t i) does not make a single single acces to element i, but rather there is a…
davidhigh
  • 14,652
  • 2
  • 44
  • 75