1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Concept Definitions</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../boost_typeerasure.html" title="Chapter 40. Boost.TypeErasure"> 10<link rel="prev" href="examples.html" title="Examples"> 11<link rel="next" href="predef.html" title="Predefined Concepts"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="examples.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeerasure.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="predef.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="boost_typeerasure.conceptdef"></a><a class="link" href="conceptdef.html" title="Concept Definitions">Concept Definitions</a> 29</h2></div></div></div> 30<p> 31 A Concept defines a set of constraints on the types that are stored in an 32 <code class="computeroutput"><a class="link" href="../boost/type_erasure/any.html" title="Class template any">any</a></code>. 33 </p> 34<p> 35 There are three kinds of concepts. 36 </p> 37<div class="orderedlist"><ol class="orderedlist" type="1"> 38<li class="listitem"> 39 The library defines a number of <a class="link" href="predef.html" title="Predefined Concepts">predefined 40 concepts</a>. Most of these are equivalent to user-defined concepts, 41 but a few require special handling. 42 </li> 43<li class="listitem"> 44 Users can define their own primitive concepts as described below. The macros 45 <code class="computeroutput"><a class="link" href="../BOOST_TYPE_ERASURE_MEMBER.html" title="Macro BOOST_TYPE_ERASURE_MEMBER">BOOST_TYPE_ERASURE_MEMBER</a></code> 46 and <code class="computeroutput"><a class="link" href="../BOOST_TYPE_ERASURE_FREE.html" title="Macro BOOST_TYPE_ERASURE_FREE">BOOST_TYPE_ERASURE_FREE</a></code> 47 define concepts of this form. 48 </li> 49<li class="listitem"> 50 Any MPL Forward Sequence whose elements are concepts is also a concept. 51 This allows concepts to be composed easily. 52 </li> 53</ol></div> 54<p> 55 Each primitive concept defines a single function. A primitive concept must 56 be a specialization of a class template, with a static member function called 57 <code class="computeroutput"><span class="identifier">apply</span></code>, which will be executed 58 when the function is dispatched by <code class="computeroutput"><a class="link" href="../boost/type_erasure/call.html" title="Function call">call</a></code>. 59 The template can only take template type parameters. non-type template parameters 60 and template template parameters are not allowed. 61 </p> 62<p> 63 The template parameters of the concept may involve placeholders. The following 64 are considered. 65 </p> 66<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 67<li class="listitem"> 68 Each template argument may be a cv and/or reference qualified placeholder 69 type. 70 </li> 71<li class="listitem"> 72 If a template argument is a function type, its arguments and return type 73 may be cv/reference qualified placeholders. 74 </li> 75</ul></div> 76<p> 77 Any other placeholders are ignored. 78 </p> 79<p> 80 A concept is instantiated by constructing an <code class="computeroutput"><a class="link" href="../boost/type_erasure/any.html" title="Class template any">any</a></code> 81 from a raw value or by constructing a <code class="computeroutput"><a class="link" href="../boost/type_erasure/binding.html" title="Class template binding">binding</a></code>. 82 When a concept is instantiated with a specific set of type bindings, each placeholder 83 is bound to a cv-unqualified non-reference type. After replacing each placeholder 84 in the template argument list with the type that it binds to, the following 85 must hold. 86 </p> 87<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 88<li class="listitem"> 89 The number of arguments of apply in the bound concept must be the same 90 as the number of arguments in the unbound concept. 91 </li> 92<li class="listitem"> 93 The arguments and return type of apply in the bound concept can be derived 94 from the corresponding arguments and the return type in the unbound concept 95 as follows: If the argument in the unbound concept is a placeholder with 96 optional cv and reference qualifiers, then the argument in the bound concept 97 can be found by replacing the placeholder. Otherwise, the argument in the 98 unbound concept must be the same as the argument in the bound concept. 99 </li> 100</ul></div> 101<pre class="programlisting"><span class="comment">// Correct.</span> 102<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span> <span class="special">=</span> <span class="identifier">_self</span><span class="special">></span> 103<span class="keyword">struct</span> <span class="identifier">foo1</span> <span class="special">{</span> 104 <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&</span> <span class="identifier">t</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">t</span><span class="special">.</span><span class="identifier">foo</span><span class="special">();</span> <span class="special">}</span> 105<span class="special">};</span> 106 107<span class="comment">// Wrong. The signature of apply is different from the</span> 108<span class="comment">// primary template</span> 109<span class="keyword">template</span><span class="special"><></span> 110<span class="keyword">struct</span> <span class="identifier">foo1</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">{</span> 111 <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">);</span> 112<span class="special">};</span> 113 114<span class="comment">// Wrong. A concept must be a template</span> 115<span class="keyword">struct</span> <span class="identifier">foo2</span> <span class="special">{</span> 116 <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">_self</span><span class="special">&);</span> 117<span class="special">};</span> 118 119<span class="comment">// Wrong. apply must be static</span> 120<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span> <span class="special">=</span> <span class="identifier">_self</span><span class="special">></span> 121<span class="keyword">struct</span> <span class="identifier">foo3</span> <span class="special">{</span> 122 <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&);</span> 123<span class="special">};</span> 124 125<span class="comment">// Wrong. apply cannot be overloaded</span> 126<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span> <span class="special">=</span> <span class="identifier">_self</span><span class="special">></span> 127<span class="keyword">struct</span> <span class="identifier">foo3</span> <span class="special">{</span> 128 <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="identifier">T</span><span class="special">&);</span> 129 <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&);</span> 130<span class="special">};</span> 131 132<span class="comment">// Wrong. Only top level placeholders are detected</span> 133<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> 134<span class="keyword">struct</span> <span class="identifier">foo4</span><span class="special">;</span> 135<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> 136<span class="keyword">struct</span> <span class="identifier">foo4</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="special">></span> <span class="special">{</span> 137 <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&);</span> 138<span class="special">};</span> 139 140<span class="comment">// Wrong. Template template parameters are not allowed.</span> 141<span class="keyword">template</span><span class="special"><</span><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span><span class="special">></span> <span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> 142<span class="keyword">struct</span> <span class="identifier">foo5</span> 143<span class="special">{</span> 144 <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="identifier">T</span><span class="special"><</span><span class="keyword">int</span><span class="special">>&);</span> 145<span class="special">};</span> 146</pre> 147</div> 148<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 149<td align="left"></td> 150<td align="right"><div class="copyright-footer">Copyright © 2011-2013 Steven Watanabe<p> 151 Distributed under the Boost Software License, Version 1.0. (See accompanying 152 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 153 </p> 154</div></td> 155</tr></table> 156<hr> 157<div class="spirit-nav"> 158<a accesskey="p" href="examples.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeerasure.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="predef.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 159</div> 160</body> 161</html> 162