Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/concept_check/creating_concepts.htm @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.3 KB
Line 
1<HTML>
2<!--
3  -- Copyright (c) Jeremy Siek and Andrew Lumsdaine 2000
4  --
5  -- Permission to use, copy, modify, distribute and sell this software
6  -- and its documentation for any purpose is hereby granted without fee,
7  -- provided that the above copyright notice appears in all copies and
8  -- that both that copyright notice and this permission notice appear
9  -- in supporting documentation.  We make no
10  -- representations about the suitability of this software for any
11  -- purpose.  It is provided "as is" without express or implied warranty.
12  -->
13<Head>
14<Title>Creating Concept Checking Classes</Title>
15<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" 
16        ALINK="#ff0000"> 
17<IMG SRC="../../boost.png" 
18     ALT="C++ Boost" width="277" height="86"> 
19
20<BR Clear>
21
22
23<h2><a name="creating-concept-checks">Creating Concept Checking Classes</a></h2>
24
25As an example of how to create a concept checking class, we look
26at how to create the corresponding checks for the
27<a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">
28RandomAccessIterator</a> concept. First, as a convention we name the
29concept checking class after the concept, and add the suffix
30``<tt>Concept</tt>''. Next we must define a member function named
31<tt>constraints()</tt> in which we will exercise the valid expressions
32of the concept. <tt>function_requires()</tt> expects this function's
33signature to appear exactly as it is appears below: a <tt>void</tt>
34non-const member function with no parameters.
35
36<p>
37The first part of the <tt>constraints()</tt> function includes
38the requirements that correspond to the <i>refinement</i> relationship
39between <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">
40RandomAccessIterator</a> and the concepts which it builds upon:
41<a href="http://www.sgi.com/tech/stl/BidirectionalIterator.html">
42BidirectionalIterator</a> and
43<a href="http://www.sgi.com/tech/stl/LessThanComparable.html">
44LessThanComparable</a>. We could have instead used
45<tt>BOOST_CLASS_REQUIRE</tt> and placed these requirements in the class
46body, however <tt>BOOST_CLASS_REQUIRE</tt> uses C++ language features that
47are less portable.
48
49<p>
50Next we check that the <tt>iterator_category</tt> of the iterator is
51either <tt>std::random_access_iterator_tag</tt> or a derived class.
52After that we write out some code that corresponds to the valid
53expressions of the <a
54href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">
55RandomAccessIterator</a> concept. Typedefs can also be added to
56enforce the associated types of the concept.
57
58<pre>
59  template &lt;class Iter&gt;
60  struct RandomAccessIteratorConcept
61  {
62    void constraints() {
63      function_requires&lt; BidirectionalIteratorConcept&lt;Iter&gt; &gt;();
64      function_requires&lt; LessThanComparableConcept&lt;Iter&gt; &gt;();
65      function_requires&lt; ConvertibleConcept&lt;
66        typename std::iterator_traits&lt;Iter&gt;::iterator_category,
67        std::random_access_iterator_tag&gt; &gt;();
68
69      i += n;
70      i = i + n; i = n + i;
71      i -= n;
72      i = i - n;
73      n = i - j;
74      i[n];
75    }
76    Iter i, j;
77    typename std::iterator_traits&lt;Iter&gt;::difference_type n;
78  };
79}
80</pre>
81
82One potential pitfall in designing concept checking classes is using
83more expressions in the constraint function than necessary. For
84example, it is easy to accidentally use the default constructor to
85create the objects that will be needed in the expressions (and not all
86concepts require a default constructor). This is the reason we write
87the constraint function as a member function of a class. The objects
88involved in the expressions are declared as data members of the class.
89Since objects of the constraints class template are never
90instantiated, the default constructor for the concept checking class
91is never instantiated. Hence the data member's default constructors
92are never instantiated (C++ Standard Section 14.7.1 9).
93
94<p>
95<a href="./concept_covering.htm">Next: Concept Covering and Archetypes</a><br>
96<a href="./using_concept_check.htm">Prev: Using Concept Checks</a>
97
98
99<br>
100<HR>
101<TABLE>
102<TR valign=top>
103<TD nowrap>Copyright &copy 2000</TD><TD>
104<A HREF="../../people/jeremy_siek.htm">Jeremy Siek</A>(<A
105HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)
106Andrew Lumsdaine</A>(<A HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
107</TD></TR></TABLE>
108
109</BODY>
110</HTML> 
Note: See TracBrowser for help on using the repository browser.