Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / doc / design / concepts.rst
1 Concepts
2 ========
3
4 All constructs in GIL are models of GIL concepts. A *concept* is a set of
5 requirements that a type (or a set of related types) must fulfill to be used
6 correctly in generic algorithms. The requirements include syntactic and
7 algorithmic guarantees. For example, GIL class ``pixel`` is a model of GIL
8 ``PixelConcept``. The user may substitute the pixel class with one of their
9 own, and, as long as it satisfies the requirements of ``PixelConcept``,
10 all other GIL classes and algorithms can be used with it.
11 See more about concepts is avaialble at
12 `Generic Programming in ConceptC++ <https://web.archive.org/web/20160324115943/http://www.generic-programming.org/languages/conceptcpp/>`_
13
14 In this document we will use a syntax for defining concepts that is described
15 in the C++ standard proposal paper
16 `[N2081] Concepts <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2081.pdf>`_.
17
18 Here are some common concepts that will be used in GIL.
19 Most of them are defined at the
20 `ConceptC++ Concept Web <https://web.archive.org/web/20160326060858/http://www.generic-programming.org/languages/conceptcpp/concept_web.php>`_:
21
22 .. code-block:: cpp
23
24   auto concept DefaultConstructible<typename T>
25   {
26       T::T();
27   };
28
29   auto concept CopyConstructible<typename T>
30   {
31       T::T(T);
32       T::~T();
33   };
34
35   auto concept Assignable<typename T, typename U = T>
36   {
37       typename result_type;
38       result_type operator=(T&, U);
39   };
40
41   auto concept EqualityComparable<typename T, typename U = T>
42   {
43       bool operator==(T x, T y);
44       bool operator!=(T x, T y) { return !(x==y); }
45   };
46
47   concept SameType<typename T, typename U> { /* unspecified */ };
48   template<typename T> concept_map SameType<T, T> { /* unspecified */ };
49
50   auto concept Swappable<typename T>
51   {
52       void swap(T& t, T& u);
53   };
54
55 Here are some additional basic concepts that GIL needs:
56
57 .. code-block:: cpp
58
59   auto concept Regular<typename T> :
60       DefaultConstructible<T>,
61       CopyConstructible<T>,
62       EqualityComparable<T>,
63       Assignable<T>,
64       Swappable<T>
65   {};
66
67   auto concept Metafunction<typename T>
68   {
69       typename type;
70   };