• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>has_pre_increment</title>
5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Chapter 1. Boost.TypeTraits">
8<link rel="up" href="../reference.html" title="Alphabetical Reference">
9<link rel="prev" href="has_pre_decrement.html" title="has_pre_decrement">
10<link rel="next" href="has_right_shift.html" title="has_right_shift">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="has_pre_decrement.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="has_right_shift.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_typetraits.reference.has_pre_increment"></a><a class="link" href="has_pre_increment.html" title="has_pre_increment">has_pre_increment</a>
28</h3></div></div></div>
29<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Rhs</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Ret</span><span class="special">=</span><span class="identifier">dont_care</span><span class="special">&gt;</span>
30<span class="keyword">struct</span> <span class="identifier">has_pre_increment</span> <span class="special">:</span> <span class="keyword">public</span> <em class="replaceable"><code><a class="link" href="integral_constant.html" title="integral_constant">true_type</a>-or-<a class="link" href="integral_constant.html" title="integral_constant">false_type</a></code></em> <span class="special">{};</span>
31</pre>
32<p>
33        <span class="bold"><strong>Inherits:</strong></span> If (i) <code class="computeroutput"><span class="identifier">rhs</span></code>
34        of type <code class="computeroutput"><span class="identifier">Rhs</span></code> can be used in
35        expression <code class="computeroutput"><span class="special">++</span><span class="identifier">rhs</span></code>,
36        and (ii) <code class="computeroutput"><span class="identifier">Ret</span><span class="special">=</span><span class="identifier">dont_care</span></code> or the result of expression
37        <code class="computeroutput"><span class="special">++</span><span class="identifier">rhs</span></code>
38        is convertible to <code class="computeroutput"><span class="identifier">Ret</span></code> then
39        inherits from <a class="link" href="integral_constant.html" title="integral_constant">true_type</a>,
40        otherwise inherits from <a class="link" href="integral_constant.html" title="integral_constant">false_type</a>.
41      </p>
42<p>
43        The default behaviour (<code class="computeroutput"><span class="identifier">Ret</span><span class="special">=</span><span class="identifier">dont_care</span></code>)
44        is to not check for the return value of prefix <code class="computeroutput"><span class="keyword">operator</span><span class="special">++</span></code>. If <code class="computeroutput"><span class="identifier">Ret</span></code>
45        is different from the default <code class="computeroutput"><span class="identifier">dont_care</span></code>
46        type, the return value is checked to be convertible to <code class="computeroutput"><span class="identifier">Ret</span></code>.
47        Convertible to <code class="computeroutput"><span class="identifier">Ret</span></code> means
48        that the return value of the operator can be used as argument to a function
49        expecting <code class="computeroutput"><span class="identifier">Ret</span></code>:
50</p>
51<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Ret</span><span class="special">);</span>
52<span class="identifier">Rhs</span> <span class="identifier">rhs</span><span class="special">;</span>
53<span class="identifier">f</span><span class="special">(++</span><span class="identifier">rhs</span><span class="special">);</span> <span class="comment">// is valid if has_pre_increment&lt;Rhs, Ret&gt;::value==true</span>
54</pre>
55<p>
56        If <code class="computeroutput"><span class="identifier">Ret</span><span class="special">=</span><span class="keyword">void</span></code>, the return type is checked to be exactly
57        <code class="computeroutput"><span class="keyword">void</span></code>.
58      </p>
59<p>
60        <span class="bold"><strong>Header:</strong></span> <code class="computeroutput"><span class="preprocessor">#include</span>
61        <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">has_pre_increment</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
62        or <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">has_operator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
63        or <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
64      </p>
65<p>
66        <span class="bold"><strong>Compiler Compatibility:</strong></span> Requires working
67        SFINAE (i.e. BOOST_NO_SFINAE is not set). Only a minority of rather old compilers
68        do not support this.
69      </p>
70<p>
71        <span class="bold"><strong>Examples:</strong></span>
72      </p>
73<div class="blockquote"><blockquote class="blockquote"><p>
74          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="identifier">Rhs</span><span class="special">,</span> <span class="identifier">Ret</span><span class="special">&gt;::</span><span class="identifier">value_type</span></code> is the type <code class="computeroutput"><span class="keyword">bool</span></code>.
75        </p></blockquote></div>
76<div class="blockquote"><blockquote class="blockquote"><p>
77          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="identifier">Rhs</span><span class="special">,</span> <span class="identifier">Ret</span><span class="special">&gt;::</span><span class="identifier">value</span></code> is a <code class="computeroutput"><span class="keyword">bool</span></code>
78          integral constant expression.
79        </p></blockquote></div>
80<div class="blockquote"><blockquote class="blockquote"><p>
81          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">value</span></code> is a <code class="computeroutput"><span class="keyword">bool</span></code>
82          integral constant expression that evaluates to <code class="computeroutput"><span class="keyword">true</span></code>.
83        </p></blockquote></div>
84<div class="blockquote"><blockquote class="blockquote"><p>
85          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">&gt;</span></code>
86          inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">true_type</a></code>.
87        </p></blockquote></div>
88<div class="blockquote"><blockquote class="blockquote"><p>
89          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;</span></code> inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">true_type</a></code>.
90        </p></blockquote></div>
91<div class="blockquote"><blockquote class="blockquote"><p>
92          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">long</span><span class="special">&gt;</span></code> inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">true_type</a></code>.
93        </p></blockquote></div>
94<div class="blockquote"><blockquote class="blockquote"><p>
95          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span></code>
96          inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">true_type</a></code>.
97        </p></blockquote></div>
98<div class="blockquote"><blockquote class="blockquote"><p>
99          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;</span></code>
100          inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">true_type</a></code>.
101        </p></blockquote></div>
102<div class="blockquote"><blockquote class="blockquote"><p>
103          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span></code>
104          inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">true_type</a></code>.
105        </p></blockquote></div>
106<div class="blockquote"><blockquote class="blockquote"><p>
107          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="keyword">int</span><span class="special">&gt;</span></code> inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">false_type</a></code>.
108        </p></blockquote></div>
109<div class="blockquote"><blockquote class="blockquote"><p>
110          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">*&gt;</span></code>
111          inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">false_type</a></code>.
112        </p></blockquote></div>
113<div class="blockquote"><blockquote class="blockquote"><p>
114          <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span></code> inherits from <code class="computeroutput"><a class="link" href="integral_constant.html" title="integral_constant">false_type</a></code>.
115        </p></blockquote></div>
116<p>
117        <span class="bold"><strong>See also:</strong></span> <a class="link" href="../category/value_traits/operators.html" title="Operator Type Traits">Operator
118        Type Traits</a>
119      </p>
120<p>
121        <span class="bold"><strong>Known issues:</strong></span>
122      </p>
123<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
124<li class="listitem">
125            This trait cannot detect whether prefix <code class="computeroutput"><span class="keyword">operator</span><span class="special">++</span></code> is public or not: if <code class="computeroutput"><span class="keyword">operator</span><span class="special">++</span></code>
126            is defined as a private member of <code class="computeroutput"><span class="identifier">Rhs</span></code>
127            then instantiating <code class="computeroutput"><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="identifier">Rhs</span><span class="special">&gt;</span></code> will produce a compiler error. For
128            this reason <code class="computeroutput"><span class="identifier">has_pre_increment</span></code>
129            cannot be used to determine whether a type has a public <code class="computeroutput"><span class="keyword">operator</span><span class="special">++</span></code>
130            or not.
131<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">A</span> <span class="special">{</span> <span class="keyword">private</span><span class="special">:</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">++();</span> <span class="special">};</span>
132<span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// error: A::operator++() is private</span>
133</pre>
134          </li>
135<li class="listitem">
136            There is an issue if the operator exists only for type <code class="computeroutput"><span class="identifier">A</span></code> and <code class="computeroutput"><span class="identifier">B</span></code>
137            is convertible to <code class="computeroutput"><span class="identifier">A</span></code>.
138            In this case, the compiler will report an ambiguous overload.
139<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">A</span> <span class="special">{</span> <span class="special">};</span>
140<span class="keyword">void</span> <span class="keyword">operator</span><span class="special">++(</span><span class="keyword">const</span> <span class="identifier">A</span><span class="special">&amp;);</span>
141<span class="keyword">struct</span> <span class="identifier">B</span> <span class="special">{</span> <span class="keyword">operator</span> <span class="identifier">A</span><span class="special">();</span> <span class="special">};</span>
142<span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// this is fine</span>
143<span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_pre_increment</span><span class="special">&lt;</span><span class="identifier">B</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// error: ambiguous overload</span>
144</pre>
145          </li>
146<li class="listitem">
147            There is an issue when applying this trait to template classes. If <code class="computeroutput"><span class="keyword">operator</span><span class="special">++</span></code>
148            is defined but does not bind for a given template type, it is still detected
149            by the trait which returns <code class="computeroutput"><span class="keyword">true</span></code>
150            instead of <code class="computeroutput"><span class="keyword">false</span></code>. Example:
151<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">has_pre_increment</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
152<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
153
154<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
155<span class="keyword">struct</span> <span class="identifier">contains</span> <span class="special">{</span> <span class="identifier">T</span> <span class="identifier">data</span><span class="special">;</span> <span class="special">};</span>
156
157<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
158<span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">++(</span><span class="keyword">const</span> <span class="identifier">contains</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&amp;</span><span class="identifier">rhs</span><span class="special">)</span> <span class="special">{</span>
159	<span class="keyword">return</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">rhs</span><span class="special">.</span><span class="identifier">data</span><span class="special">);</span>
160<span class="special">}</span>
161
162<span class="keyword">class</span> <span class="identifier">bad</span> <span class="special">{</span> <span class="special">};</span>
163<span class="keyword">class</span> <span class="identifier">good</span> <span class="special">{</span> <span class="special">};</span>
164<span class="keyword">bool</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">good</span><span class="special">&amp;)</span> <span class="special">{</span> <span class="special">}</span>
165
166<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
167	<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">&lt;&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">boolalpha</span><span class="special">;</span>
168	<span class="comment">// works fine for contains&lt;good&gt;</span>
169	<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">&lt;&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_pre_increment</span><span class="special">&lt;</span> <span class="identifier">contains</span><span class="special">&lt;</span> <span class="identifier">good</span> <span class="special">&gt;</span> <span class="special">&gt;::</span><span class="identifier">value</span><span class="special">&lt;&lt;</span><span class="char">'\n'</span><span class="special">;</span> <span class="comment">// true</span>
170	<span class="identifier">contains</span><span class="special">&lt;</span><span class="identifier">good</span><span class="special">&gt;</span> <span class="identifier">g</span><span class="special">;</span>
171	<span class="special">++</span><span class="identifier">g</span><span class="special">;</span> <span class="comment">// ok</span>
172	<span class="comment">// does not work for contains&lt;bad&gt;</span>
173	<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">&lt;&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_pre_increment</span><span class="special">&lt;</span> <span class="identifier">contains</span><span class="special">&lt;</span> <span class="identifier">bad</span> <span class="special">&gt;</span> <span class="special">&gt;::</span><span class="identifier">value</span><span class="special">&lt;&lt;</span><span class="char">'\n'</span><span class="special">;</span> <span class="comment">// true, should be false</span>
174	<span class="identifier">contains</span><span class="special">&lt;</span><span class="identifier">bad</span><span class="special">&gt;</span> <span class="identifier">b</span><span class="special">;</span>
175	<span class="special">++</span><span class="identifier">b</span><span class="special">;</span> <span class="comment">// compile time error</span>
176	<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
177<span class="special">}</span>
178</pre>
179          </li>
180<li class="listitem">
181            <code class="computeroutput"><span class="keyword">volatile</span></code> qualifier is not
182            properly handled and would lead to undefined behavior
183          </li>
184</ul></div>
185<p>
186        <span class="bold"><strong>Known issues:</strong></span>
187      </p>
188<p>
189        For modern compilers (those that support arbitrary SFINAE-expressions and
190        decltype/declval) this trait offers near perfect detection. In this situation
191        the macro <code class="computeroutput"><span class="identifier">BOOST_TT_HAS_ACCURATE_BINARY_OPERATOR_DETECTION</span></code>
192        will be defined after including <code class="literal">&lt;boost/type_traits/has_pre_increment.hpp&gt;</code>.
193        Please note however, that detection is based on function signature only,
194        in the case that the operator is a function template then has_pre_increment
195        cannot perform introspection of the template function body to ensure that
196        the type meets all of the conceptual requirements of the actual code.
197      </p>
198<p>
199        For older compilers (<code class="computeroutput"><span class="identifier">BOOST_TT_HAS_ACCURATE_BINARY_OPERATOR_DETECTION</span></code>
200        not defined) then there are a number of issues:
201      </p>
202<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
203<li class="listitem">
204            This trait cannot detect whether unary <code class="computeroutput"><span class="keyword">operator</span></code>++
205            is public or not: if <code class="computeroutput"><span class="keyword">operator</span></code>++
206            is defined as a private class member of type T then instantiating <code class="literal">has_pre_increment&lt;T&gt;</code>
207            will produce a compiler error. For this reason <code class="literal">has_pre_increment</code>
208            cannot be used to determine whether a type has a public <code class="computeroutput"><span class="keyword">operator</span></code>++ or not.
209          </li>
210<li class="listitem">
211            <code class="computeroutput"><span class="keyword">volatile</span></code> qualifier is not
212            properly handled and would lead to undefined behavior
213          </li>
214<li class="listitem">
215            Capturless lambdas are not supported and will lead to a compiler error
216            if has_pre_increment is instantiated on the lambda type.
217          </li>
218<li class="listitem">
219            Scoped enum types are not supported and will lead to a compiler error
220            if has_pre_increment is instantiated on such a type.
221          </li>
222</ul></div>
223</div>
224<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
225<td align="left"></td>
226<td align="right"><div class="copyright-footer">Copyright © 2000, 2011 Adobe Systems Inc, David Abrahams,
227      Frederic Bron, Steve Cleary, Beman Dawes, Glen Fernandes, Aleksey Gurtovoy,
228      Howard Hinnant, Jesse Jones, Mat Marcus, Itay Maman, John Maddock, Alexander
229      Nasonov, Thorsten Ottosen, Roman Perepelitsa, Robert Ramey, Jeremy Siek, Robert
230      Stewart and Steven Watanabe<p>
231        Distributed under the Boost Software License, Version 1.0. (See accompanying
232        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>)
233      </p>
234</div></td>
235</tr></table>
236<hr>
237<div class="spirit-nav">
238<a accesskey="p" href="has_pre_decrement.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="has_right_shift.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
239</div>
240</body>
241</html>
242