I was asked by a student what is the best concise reading about C++ template meta-programming and compile-time features like enforcements ("constraints").
I would strongly recommend to read Chapter 3 of "Modern C++ Design" by Andrei Alexandrescu.
In this Chapter Alexandrescu demonstrates how one can implement Lisp like lists and functions to manipulate them which are available at compile time for automatic code generation.
Basically, he implements a Typelist - a template to store a list of types and a set of functions like a length of the list, indexed access, searching, appending, erasing duplicates, partially ordering (according to subclass hierarchy). All functions are implemented as recursive compile-time routines.
The chapter is well-written and can be a good introduction into functional programming primitives into C++ compile-time programming.
Besides that, the Chapter gives a good demonstration how these features can be used.
Another excellent introduction into C++ compile-time features is "Compile-Time Contracts: Constraints" section of "Imperfect C++ Practical Solutions for Real-Life Programming" by Willson.
This section contains several examples of implementation of compile-time constraints in C++. For example, such constraints as a class A must be a subclass of class B, type C most be pod, typer A and type B must be the same size, et cetera. These features are very useful for generic programming and other chapters of the same book contain an excellent demo.
I would strongly recommend to read Chapter 3 of "Modern C++ Design" by Andrei Alexandrescu.
In this Chapter Alexandrescu demonstrates how one can implement Lisp like lists and functions to manipulate them which are available at compile time for automatic code generation.
Basically, he implements a Typelist - a template to store a list of types and a set of functions like a length of the list, indexed access, searching, appending, erasing duplicates, partially ordering (according to subclass hierarchy). All functions are implemented as recursive compile-time routines.
The chapter is well-written and can be a good introduction into functional programming primitives into C++ compile-time programming.
Besides that, the Chapter gives a good demonstration how these features can be used.
Another excellent introduction into C++ compile-time features is "Compile-Time Contracts: Constraints" section of "Imperfect C++ Practical Solutions for Real-Life Programming" by Willson.
This section contains several examples of implementation of compile-time constraints in C++. For example, such constraints as a class A must be a subclass of class B, type C most be pod, typer A and type B must be the same size, et cetera. These features are very useful for generic programming and other chapters of the same book contain an excellent demo.
