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

Persistent expression templates with unique_ptr and matrices

I want to use expression templates to create a tree of objects that persists across statement. Building the tree initially involves some computations with the Eigen linear algebra library. The persistent expression template will have additional…
2
votes
3 answers

Templated functions in templated classes

I am constructing a library that makes use of expression templates, where I make heavily use of templated functions in classes. All my code is running and recently I decided to make the main class templated to allow for using it on data of different…
Chiel
  • 6,006
  • 2
  • 32
  • 57
2
votes
1 answer

Constructing a qi::rule with a function attribute

I'm trying to create a rule that returns a function constructed by currying a Phoenix expression. E.g., start = int_[_val = xxx]; rule start; What should xxx be so that parsing the string…
2
votes
1 answer

nested std::forward_as_tuple and segmentation fault

My actual problem is a lot more complicated and it seems extremely difficult to give a short concrete example here to reproduce it. So I am posting here a different small example that may be relevant, and its discussion may help in the actual…
2
votes
0 answers

What prevents the compiler do a peephole optimization on expression templates?

I have the code listed below: for(auto i =0;i objects, k is the size of va1 and va2. What I am…
2
votes
2 answers

Develop a static library in Visual C++ for efficient numerical computation

I've the following problem: I need to devlop a static library (*.lib) in visual C++ for efficient numerical computation. I've started defining a new template class "Matrix" and I've read that best efficiency may be achieved with expression…
1
vote
1 answer

CRTP: Compiler dependent issue with Expression Template

I incurred in a compiler dependent issue with the following code (stored in crtp.cc): #include #include #include template < class Derived > class AlgebraicVectorExpression { public: typedef…
Massimiliano
  • 7,842
  • 2
  • 47
  • 62
1
vote
1 answer

Can I generalize this set of functions into one that takes a variable length argument list (e.g. by using a parameter pack)

I want to know if there is a way to generalize this set of functions into one that takes an arbitrary number of arguments. The specifics of Var and LetN shouldn't be needed here. (This is from some expression template building code. LetN creates a…
asynth
  • 214
  • 2
  • 8
1
vote
1 answer

C++ expression templates for slices

I'm re-writing a c++ FEM library in which I used expression template. In the previous version I was able to do, for example BilinearForm s(mesh, Vh, Vh, [&gp, &mesh](const TrialFunction& u, const TestFunction& v) -> double { return…
1
vote
0 answers

boost::multiprecision::cpp_dec_float - Expresison template surprising behavior when using "auto const"?

I've spent the better part of the day tracking a problem down to this example: #include #include int main () { typedef…
namezero
  • 2,203
  • 3
  • 24
  • 37
1
vote
1 answer

Can expression templates using references to temporaries be re-useable?

I am trying to wrap my head around expression templates. In the wikipedia article, an example is given, where an expression template VecSum stores const references to its two operands. A Vec is an expression template that wraps an…
joergbrech
  • 2,056
  • 1
  • 5
  • 17
1
vote
2 answers

C++ Finite difference differentiation - design

let A be: class A { std::vector values_; public: A(const std::vector &values) : values_(values){}; void bumpAt(std::size_t i, const double &df) { values_[i] += df; virtual method1(); virtual method2(); ... } class…
mastro
  • 619
  • 1
  • 8
  • 17
1
vote
1 answer

C++ Operator Modification/Metaprogramming Strategies for Less-Verbose Syntax

I am learning about template meta programming and expression templates in C++ right now, so as an exercise, I am creating a linear algebra library to practice the concepts I am learning. So far, my library has a complete list of non-member operator…
1
vote
4 answers

Avoiding need for #define with expression templates

With the following code, "hello2" is not displayed as the temporary string created on Line 3 dies before Line 4 is executed. Using a #define as on Line 1 avoids this issue, but is there a way to avoid this issue without using #define? (C++11 code is…
Clinton
  • 22,361
  • 15
  • 67
  • 163
1
vote
1 answer

Nesting of subexpressions in expression templates

We are writing an expression template library to handle operations on values with a sparse gradient vector (first order automatic differentiation). I am trying to figure out how to make it possible to nest sub-expressions by reference or values…