EXPRESSION TEMPLATES WITH CONCEPTS
ETC++ Library
 
 
INTRODUCTION
 

Concepts are likely to be introduced in a future C++ standard. They can be used for constraining template parameters, which enables checking requirements on template parameters sooner in the compilation process, and thus providing more intelligible error messages to the user.

They can also be used in the specialization of templates, thus leading to a better control over the selection of the most appropriate version of a template for a given instantiation. This latter aspect offers new possibilities in the design of template libraries, as it enhances the specialization mechanism of templates, and set it up as a solid alternative to inheritance when static binding can replace dynamic binding.

The research report addresses the design of expression templates (i.e. templates that represent expressions and are usually built through operator overloading) that are useful to develop an embedded domain specific language (EDSL), and can speed up the evaluation of an expression by delaying the evaluation of intermediate operations to avoid unnecessary temporary objects.

The ETC++ library, "Expression Templates with Concepts in C++", implements the solution presented in the report. The source code of the library, the examples discussed in the report, and an application of the library to linear programming are available here.

 
ETC++ LIBRARY
 

The library is standard C++11 code and is composed of three files respectively to model expression templates, overload operators, and evaluate expressions.

  • etc_expression.hpp
    Provides metaprogramming elements to model expression templates with concepts.

  • etc_operator.hpp
    Provides metaprogramming elements to overload operators for expression templates.

  • etc_visitor.hpp
    Provides metaprogramming elements to evaluate expression templates with concepts.

 
EXAMPLES
 

The examples discussed in the report are presented here. The first one introduces the syntax of the used to manipulate concepts. The two next examples illustrate the use of expression templates for array operations, the first one with the classical approach and the second one with the ETC++ library. The last example shows how to use expression templates when type erasure is needed.

  • c4ts.cpp
    Shows an example of concept-based specialization with the .

  • et_classic.cpp
    Shows an example of expression templates for array operations with the classical approach.

  • et_concepts.cpp
    Shows an example of expression templates for array operations with the ETC++ library.

  • type_erasure.cpp
    Shows an example of expression templates using type erasure with the ETC++ library.

 
APPLICATION TO LINEAR PROGRAMMING
 

The library has been used to design an EDSL for linear programming as presented in the report. This example is composed of several files that implement the structure of a linear program (variables, constraints, objective) and define an EDSL to build linear expressions, constraints and objectives to insert into a linear program.

With only 8 specializations of the visitor used to evaluate expression templates, verification, building and simplification of a linear expression or constraint have been defined with our library. Thanks to concepts, the evaluation of an expression is fully controlled: the selection of a specialized version of the visitor is only possible if the operand models the specified concept, and assertions based on concepts allow additional controls to validate the syntax.

 
DOWNLOAD
 

You can download the of the library and of all the additional files presented here. It has been tested successfully on GNU GCC and Clang compilers.