CONTROLLING TEMPLATE SPECIALIZATION
WITH CONCEPTS IN C++
C4TS++ Library
 
 
INTRODUCTION
 

In generic programming, software components are parameterized on types. When available, a static specialization mechanism allows selecting, for a given set of parameters, a more suitable version of a generic component than its primary version. The normal C++ template specialization mechanism is based on the type pattern of the parameters, which is not always the best way to guide the specialization process: type patterns are missing some information on types that could be relevant to define specializations.

The notion of a "concept", which represents a set of requirements (including syntactic and semantic aspects) for a type, is known to be an interesting approach to control template specialization. For many reasons, concepts were dropped from C++11 standard, the research report therefore describes template metaprogramming techniques for declaring concepts, "modeling" relationships (meaning that a type fulfills the requirements of a concept), and "refinement" relationships (meaning that a concept refines the requirements of another concept).

The C4TS++ library, that stands for "Concepts for Template Specialization in C++", implements the solution presented in the report. The source code of the library, the examples discussed in the report, and the generator of random instances used to measure the performance of the library are available here.

 
C4TS++ LIBRARY
 

The library is standard C++ code and two versions are available: the plain version that is meant for understanding, and that is presented in the report; and the optimized version that improves the compile time. The files listed below are part of the plain version.

  • gnx_basic.hpp
    Provides basic metaprogramming elements.

  • gnx_list.hpp
    Provides metaprogramming elements to manipulate typelists.

  • gnx_concept.hpp
    Provides metaprogramming elements for concept-based specialization.

 
EXAMPLES
 

The examples discussed in the report are presented here. The first three examples address the issue of providing a generic class for the serialization of objects, using the traditional template specialization based on type patterns, and two other existing solutions. The last two examples use the C4TS++ library, respectively to address the serialization issue and to illustrate the proposed novel mechanism to select the specializations.

 
PERFORMANCE ANALYSIS
 

Experiments have been performed to measure the compile time overhead induced by the library. Instances of concept-based specialization have been randomly generated with various numbers of concepts and modeling/refinement relationships.

  • generate_instance.cpp
    Generates a random instance of concept-based specialization for the C4TS++ library. The number n of concepts, the number r of relationships, the metafunction x to be tested and the number p of calls to this metafunction must be provided.

The experiments confirm the theoretical performance analysis of the report: using concepts to control template specialization induces an overhead of O(n2 + rn) operations at compile time. Even if a quadratic dependence on the number of concepts has been identified, the compile time is reasonable for many applications: compiling 50 specializations with 50 concepts and 250 modeling/refinement relationships on an average computer (e.g. Intel Core 2 Duo T8100 2.1 GHz) requires less than 5 seconds.

 
DOCUMENTS
 

Technical reports provide supplementary information on the design and use of the C4TS++ library.


  • Explains how logical operations on concepts were added in the C4TS++ library.


  • Presents solutions to prevent some "Schrödinger's Cat" effect with templates, especially when using the C4TS++ library. The of the examples discussed in the document is provided.

 
DOWNLOAD
 

You can download the of the library and of all the additional files presented here. It has been tested successfully on several compilers: GNU GCC, Microsoft Visual C++ and Embarcadero C++.