# The Boost Concept Check Library (BCCL)

## header `boost/concept_check.hpp`
and `boost/concept_archetype.hpp`

Generic programming in C++ is characterized by the use of template parameters to represent abstract data types (or ``concepts''). However, the C++ language itself does not provide a mechanism for the writer of a class or function template to explicitly state what concept the user-supplied template argument should model (or conform to). The common practice is to name the template parameter after the required concept as a hint to the user and to state the concept requirements in the documentation. However, often times the requirements are vague, incorrect, or nonexistent, which is quite a problem for the user, since he or she will not know exactly what kind of input is expected by the template. Furthermore, the following problems occur:

- Compiler error messages resulting from incorrect template arguments can be particularly difficult to decipher. Often times the error does not point to the location of the template call-site, but instead exposes the internals of the template, which the user should never have to see.
- The documented concept requirements may not fully cover the template, meaning the user could get a compiler error even though the supplied template arguments meet the documented requirements.
- The documented concept requirements may be too stringent, requiring more than is really needed by the template.
- The requirements are not explicitly stated in the code, which makes the code harder to understand. Also, the code may get out-of-sync with the documented requirements.

The Boost Concept Checking Library provides:

- A mechanism for inserting compile-time checks of template parameters.
- A framework for specifying concept requirements though concept checking classes.
- A mechanism for verifying that concept requirements cover the template.
- A suite of concept checking classes and archetype classes that match the concept requirements in the C++ Standard Library.

The mechanisms use standard C++ and introduce no run-time overhead. The main cost of using the mechanism is in compile-time.

Any programmer writing class or function templates ought to make concept checking a normal part of their code writing routine. A concept check should be inserted for each template parameter in a component's public interface. If the concept is one of the ones from the Standard Library, then simply use the matching concept checking class in the BCCL. If not, then write a new concept checking class - after all, they are typically only a few lines long. For new concepts, a matching archetype class should also be created, which is a minimal skeleton-implementation of the concept

The documentation is organized into the following sections.

1. Introduction
2. Motivating Example
3. History
4. Publications
5. Acknowledgements
6. Using Concept Checks
7. Creating Concept Checking Classes
8. Concept Covering and Archetypes
9. Programming With Concepts
10. Implementation
11. Reference

Jeremy Siek contributed this library. Beman Dawes managed the formal review.

## Introduction

A concept is a set of requirements (valid expressions, associated types, semantic invariants, complexity guarantees, etc.) that a type must fulfill to be correctly used as arguments in a call to a generic algorithm. In C++, concepts are represented by formal template parameters to function templates (generic algorithms). However, C++ has no explicit mechanism for representing concepts --- template parameters are merely placeholders. By convention, these parameters are given names corresponding to the concept that is required, but a C++ compiler does not enforce compliance to the concept when the template parameter is bound to an actual type.

Naturally, if a generic algorithm is invoked with a type that does not fulfill at least the syntactic requirements of the concept, a compile-time error will occur. However, this error will not per se reflect the fact that the type did not meet all of the requirements of the concept. Rather, the error may occur deep inside the instantiation hierarchy at the point where an expression is not valid for the type, or where a presumed associated type is not available. The resulting error messages are largely uninformative and basically impenetrable.

What is required is a mechanism for enforcing ``concept safety'' at (or close to) the point of instantiation. The Boost Concept Checking Library uses some standard C++ constructs to enforce early concept compliance and that provides more informative error messages upon non-compliance.

Note that this technique only addresses the syntactic requirements of concepts (the valid expressions and associated types). We do not address the semantic invariants or complexity guarantees, which are also part of concept requirements..

## Motivating Example

We present a simple example to illustrate incorrect usage of a template library and the resulting error messages. In the code below, the generic `std::stable_sort()` algorithm from the Standard Template Library (STL)[3, 4,5] is applied to a linked list.

      bad_error_eg.cpp:
       1  #include <list>
       2  #include <algorithm>
       3
       4  int main(int, char*[]) {
       5    std::list<int> v;
       6    std::stable_sort(v.begin(), v.end());
       7    return 0;
       8  }

Here, the `std::stable_sort()` algorithm is prototyped as follows:

      template <class RandomAccessIterator>
      void stable_sort(RandomAccessIterator first, RandomAccessIterator last);

Attempting to compile this code with Gnu C++ produces the following compiler error. The output from other compilers is listed in the Appendix.

    stl_algo.h: In function `void __merge_sort_loop<_List_iterator
      <int,int &,int *>, int *, int>(_List_iterator<int,int &,int *>,
      _List_iterator<int,int &,int *>, int *, int)':
    stl_algo.h:1448:   instantiated from `__merge_sort_with_buffer
      <_List_iterator<int,int &,int *>, int *, int>(
       _List_iterator<int,int &,int *>, _List_iterator<int,int &,int *>,
       int *, int *)'
    stl_algo.h:1485:   instantiated from `__stable_sort_adaptive<
      _List_iterator<int,int &,int *>, int *, int>(_List_iterator
      <int,int &,int *>, _List_iterator<int,int &,int *>, int *, int)'
    stl_algo.h:1524:   instantiated from here
    stl_algo.h:1377: no match for `_List_iterator<int,int &,int *> & -
      _List_iterator<int,int &,int *> &'

In this case, the fundamental error is that `std:list::iterator` does not model the concept of RandomAccessIterator. The list iterator is only bidirectional, not fully random access (as would be a vector iterator). Unfortunately, there is nothing in the error message to indicate this to the user.

To a C++ programmer having enough experience with template libraries the error may be obvious. However, for the uninitiated, there are several reasons why this message would be hard to understand.

1. The location of the error, line 6 of `bad_error_eg.cpp` is not pointed to by the error message, despite the fact that Gnu C++ prints up to 4 levels deep in the instantiation stack.
2. There is no textual correlation between the error message and the documented requirements for `std::stable_sort()` and for RandomAccessIterator.
3. The error message is overly long, listing functions internal to the STL that the user does not (and should not!) know or care about.
4. With so many internal library functions listed in the error message, the programmer could easily infer that the error is due to the library, rather than to his or her own code.

The following is an example of what we might expect from a more informative message (and is in fact what the Boost Concept Checking Library produces):

    boost/concept_check.hpp: In method `void LessThanComparableConcept
      <_List_iterator<int,int &,int *> >::constraints()':
    boost/concept_check.hpp:334:   instantiated from `RandomAccessIteratorConcept
      <_List_iterator<int,int &,int *> >::constraints()'
    bad_error_eg.cpp:6:   instantiated from `stable_sort<_List_iterator
      <int,int &,int *> >(_List_iterator<int,int &,int *>,
      _List_iterator<int,int &,int *>)'
    boost/concept_check.hpp:209: no match for `_List_iterator<int,int &,int *> &
      < _List_iterator<int,int &,int *> &'

This message rectifies several of the shortcomings of the standard error messages.

- The location of the error, `bad_error_eg.cpp:6` is specified in the error message.
- The message refers explicitly to concepts that the user can look up in the STL documentation ( RandomAccessIterator).
- The error message is now much shorter and does not reveal internal STL functions.
- The presence of `concept_check.hpp` and `constraints()` in the error message alerts the user to the fact that the error lies in the user code and not in the library implementation.

## History

An earlier version of this concept checking system was developed by the author while working at SGI in their C++ compiler and library group. The earlier version is now part of the SGI STL distribution. The boost concept checking library differs from the concept checking in the SGI STL in that the definition of concept checking classes has been greatly simplified, at the price of less helpful verbiage in the error messages.

## Publications

- C++ Template Workshop 2000, Concept Checking

## Acknowledgements

The idea to use function pointers to cause instantiation is due to Alexander Stepanov. I am not sure of the origin of the idea to use expressions to do up-front checking of templates, but it did appear in D&E[ 2]. Thanks to Matt Austern for his excellent documentation and organization of the STL concepts, upon which these concept checks are based. Thanks to Boost members for helpful comments and reviews.

Next: Using Concept Checks

Copyright © 2000 | Jeremy Siek, Univ.of Notre Dame (jsiek@lsc.nd.edu)