Concepts in Generic Programming

Generic programming is supported in C++ through the notion of templates. The templates provide abstraction while retaining the opportunity for optimal performance.

A Concept is a set of requirements on a type and is represented in this book in Pascal-Case (a style of combining words into keywords by capitalizing the first letters in each word). The requirements may be syntactic or semantic. For example, the Concept of Sortable could be defined as a set of requirements that enable an array to be sorted. A type T would be Sortable if:

  • x < y returns a Boolean value, and represents a total order on items of type T.

  • swap(x,y) swaps items x and y.

You can write a sorting template function in C++ and be assured that it can sort an array of any type that is Sortable.

We describe the requirements you should observe on types for Threading Building Blocks types in this book as Concepts. When these constraints are followed, Threading Building Blocks will work well. When they are violated, you may get a cryptic compiler error message or occasionally inefficient or incorrect code. There is a great deal of interest in designing a type system for C++ that can check template arguments for errors. In the future, we may have extensions to C++ that allow more type constraints in the template definitions. This will lead to more safety and better error messages from the compiler.

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.