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
1
vote
1 answer

Expression templates with a hierarchy defined on the nodes in the syntax tree

I found an excellent explanation of expression templates here. In the article, we find a basic expression template implementation for arithmetic, as follows (slightly adapted): #include template struct plus { T…
Nibor
  • 1,236
  • 9
  • 23
1
vote
2 answers

c++ Force implicit conversion on pass as argument

I have problem with implicit conversions in C++. I'm trying to create some Expression template for vector arithmetics (I know that same libraries already exists. I'm just learning C++ so I wanted to try something with templates). I would like to…
Payne
  • 456
  • 6
  • 21
1
vote
0 answers

Delay evaluation of an expression using std::future

I have a class called Expr which uses expression template methodology, that could be used to build expressions like this: Expr> v(Mat(10,10)); v(0,0)*3 + v(2,1)*v(1,2) >> val; The expression is evaluated when operator>>(T& val) is…
Sayan
  • 2,662
  • 10
  • 41
  • 56
1
vote
1 answer

Use the return type of a method as an argument type of another method in a curiously recurring template class

Please consider the following code snippet: template class vector_expression { public: auto size() const { return static_cast(*this).size(); } auto operator[](/* type equal to E::size_type */ i) const { …
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
1
vote
1 answer

Expression template and summation symbol

I'm writing a small library for quantum mechanics and I want to use expression template to form operator expressions. Especially forming the Hamiltonian with expression template. I basically followed this source to construct the code and…
1
vote
1 answer

C++ Expression Templates - Why the base class?

I recently stumbled across expression templates in C++. There is one thing that I do not quite understand about their implementation, and that is why the base class is necessary (from which all other objects relating to template expression derive in…
Konrad
  • 2,207
  • 4
  • 20
  • 31
1
vote
0 answers

Using Eigen with expression templates parameters slows down compiling time

I am writing a C++ library in which I use functions that take as parameters expression templates in Eigen3. Basically the definitions of my functions are similar to template /* return type */ f(Eigen::MatrixBase param) { //…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
1
vote
1 answer

Expression template with CRTP as lvalue

I'm writing a library that uses expression templates with CRTP. The source files can be found here: https://github.com/mspraggs/pyQCD/tree/master/lib/include/base The expression templates are based on the example given in the Wikipedia article on…
orentago
  • 578
  • 1
  • 7
  • 13
1
vote
2 answers

Expression Template implementation not being optimized

I'm trying to understand the concept of expression templates in C++, as such I've cobbled together pieces of example code etc to produce a simple vector and associated expression template infrastructure to support only binary operators…
1
vote
1 answer

Expression Templates + CRTP + AMP == kernel generation

I have recently discovered the sheer awesomeness of expression templates and have come to a somewhat satisfactory level of understanding and skills in their usage, however I want to make a new use of the idiom. I would skip the lengthy story about…
Meteorhead
  • 480
  • 3
  • 13
1
vote
2 answers

Data member and rvalue life-time

Somehow inspired by the expression template code in Expression templates and C++11, written by Paul Preney, I decided to test the following: template struct X { X(T t) : t(std::forward(t)) {} T t; }; template
A.L.
  • 1,133
  • 1
  • 12
  • 19
1
vote
1 answer

Implementing the A(:,k)=b; Matlab-like syntax in a C++ matrix library

I have developed an expression templates-based C++ matrix class of my own. I have overloaded the () operator so that I can read or write element matrices as, for example, cout << A(i,j) << endl; and A(i,j)=b; respectively. I have also implemented…
Vitality
  • 20,705
  • 4
  • 108
  • 146
1
vote
0 answers

Expression template, addition of a variable and a value

I have an expression template which adds a variable with a constant in a given expression. I want to convert that to add the variable with any given number. Structs for constants and variables: struct Var { int operator () (int v) { return v;…
1
vote
1 answer

Intel C++ compiler can't handle deep templates?

I have a project in C++ using marray library. For now it compiles and runs quite fine with MinGW g++ 4.7 and msvc2010 on Windows 7 x64 and also with g++ 4.7 on Linux Mint x64. I decided to give a try to Intel C++ compiler v. 12.1.4 for Linux. It was…
Dmitry
  • 3,063
  • 2
  • 22
  • 32
0
votes
1 answer

need non-const expression classes in expression templates

I'm trying to write an expression template and I've run into a problem I don't know how to solve. I've read C++ Templates: The Complete Guide but they don't seem to address this question. As an example, consider a expression template for a set type…
Cotton Seed
  • 363
  • 3
  • 13