utility.htm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <title>Header boost/utility.hpp Documentation</title>
  5. </head>
  6. <body bgcolor="#FFFFFF" text="#000000">
  7. <h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align="center" WIDTH="277" HEIGHT="86">Header
  8. <a href="../../boost/utility.hpp">boost/utility.hpp</a></h1>
  9. <p>The entire contents of the header <code><a href="../../boost/utility.hpp">&lt;boost/utility.hpp&gt;</a></code>
  10. are in <code>namespace boost</code>.</p>
  11. <h2>Contents</h2>
  12. <ul>
  13. <li>
  14. Class templates supporting the <a href="doc/html/base_from_member.html">
  15. base-from-member idiom</a></li>
  16. <li>
  17. Function templates <a href="../core/doc/html/core/checked_delete.html">checked_delete() and
  18. checked_array_delete()</a> (moved to the Boost.Core library)</li>
  19. <li>
  20. Function templates <a href="../iterator/doc/html/iterator/algorithms/next_prior.html">next() and prior()</a> (moved to the Boost.Iterator library)</li>
  21. <li>
  22. Class <a href="../core/doc/html/core/noncopyable.html">noncopyable</a> (moved to the Boost.Core library)</li>
  23. <li>
  24. Function template <a href="../core/doc/html/core/addressof.html">addressof()</a> (moved to the Boost.Core library)</li>
  25. <li>Class template <a href="#result_of">result_of</a></li>
  26. <li>
  27. Macro <a href="#BOOST_BINARY">BOOST_BINARY</a></li>
  28. <li><a href="index.html">Other utilities not part of <code>utility.hpp</code></a></li>
  29. </ul>
  30. <h2>
  31. <h2><a name="result_of">Class template
  32. result_of</a></h2> <p>The class template
  33. <code>result_of</code> helps determine the type of a
  34. call expression. For example, given an lvalue <code>f</code> of
  35. type <code>F</code> and lvalues <code>t1</code>,
  36. <code>t2</code>, ..., <code>t<em>N</em></code> of
  37. types <code>T1</code>, <code>T2</code>, ...,
  38. <code>T<em>N</em></code>, respectively, the type
  39. <code>result_of&lt;F(T1, T2, ...,
  40. T<em>N</em>)&gt;::type</code> defines the result type
  41. of the expression <code>f(t1, t2,
  42. ...,t<em>N</em>)</code>. This implementation permits
  43. the type <code>F</code> to be a function pointer,
  44. function reference, member function pointer, or class
  45. type. By default, <em>N</em> may be any value between 0 and
  46. 16. To change the upper limit, define the macro
  47. <code>BOOST_RESULT_OF_NUM_ARGS</code> to the maximum
  48. value for <em>N</em>. Class template <code>result_of</code>
  49. resides in the header <code>&lt;<a
  50. href="../../boost/utility/result_of.hpp">boost/utility/result_of.hpp</a>&gt;</code>.</p>
  51. <p>If your compiler's support for <code>decltype</code> is
  52. adequate, <code>result_of</code> automatically uses it to
  53. deduce the type of the call expression, in which case
  54. <code>result_of&lt;F(T1, T2, ...,
  55. T<em>N</em>)&gt;::type</code> names the type
  56. <code>decltype(boost::declval&lt;F&gt;()(boost::declval&lt;T1&gt;(),
  57. boost::declval&lt;T2&gt;(), ...,
  58. boost::declval&lt;T<em>N</em>&gt;()))</code>, as in the
  59. following example.</p>
  60. <blockquote>
  61. <pre>struct functor {
  62. template&lt;class T&gt;
  63. T operator()(T x)
  64. {
  65. return x;
  66. }
  67. };
  68. typedef boost::result_of&lt;
  69. functor(int)
  70. &gt;::type type; // type is int</pre>
  71. </blockquote>
  72. <p>You can test whether <code>result_of</code> is using
  73. <code>decltype</code> by checking if the macro
  74. <code>BOOST_RESULT_OF_USE_DECLTYPE</code> is defined after
  75. including <code>result_of.hpp</code>. You can also force
  76. <code>result_of</code> to use <code>decltype</code> by
  77. defining <code>BOOST_RESULT_OF_USE_DECLTYPE</code> prior
  78. to including <code>result_of.hpp</code>.</p>
  79. <p>If <code>decltype</code> is not used,
  80. then automatic result type deduction of function
  81. objects is not possible. Instead, <code>result_of</code>
  82. uses the following protocol to allow the programmer to
  83. specify a type. When <code>F</code> is a class type with a
  84. member type <code>result_type</code>,
  85. <code>result_of&lt;F(T1, T2, ...,
  86. T<em>N</em>)&gt;::type</code> is
  87. <code>F::result_type</code>. When <code>F</code> does
  88. not contain <code>result_type</code>,
  89. <code>result_of&lt;F(T1, T2, ...,
  90. T<em>N</em>)&gt;::type</code> is <code>F::result&lt;F(T1,
  91. T2, ..., T<em>N</em>)&gt;::type</code> when
  92. <code><em>N</em> &gt; 0</code> or <code>void</code>
  93. when <code><em>N</em> = 0</code>. Note that it is the
  94. responsibility of the programmer to ensure that
  95. function objects accurately advertise their result
  96. type via this protocol, as in the following
  97. example.</p>
  98. <blockquote>
  99. <pre>struct functor {
  100. template&lt;class&gt; struct result;
  101. template&lt;class F, class T&gt;
  102. struct result&lt;F(T)&gt; {
  103. typedef T type;
  104. };
  105. template&lt;class T&gt;
  106. T operator()(T x)
  107. {
  108. return x;
  109. }
  110. };
  111. typedef boost::result_of&lt;
  112. functor(int)
  113. &gt;::type type; // type is int</pre>
  114. </blockquote>
  115. <p>Since <code>decltype</code> is a new language
  116. feature recently standardized in C++11,
  117. if you are writing a function object
  118. to be used with <code>result_of</code>, for
  119. maximum portability, you might consider following
  120. the above protocol even if your compiler has
  121. proper <code>decltype</code> support. If you wish to continue to
  122. use the protocol on compilers that
  123. support <code>decltype</code>, there are two options:
  124. You can use <code>boost::tr1_result_of</code>, which is also
  125. defined in <code>&lt;<a href="../../boost/utility/result_of.hpp">boost/utility/result_of.hpp</a>&gt;</code>.
  126. Alternatively, you can define the macro
  127. <code>BOOST_RESULT_OF_USE_TR1</code>, which causes
  128. <code>result_of</code> to use the protocol described
  129. above instead of <code>decltype</code>. If you choose to
  130. follow the protocol, take care to ensure that the
  131. <code>result_type</code> and
  132. <code>result&lt;&gt;</code> members accurately
  133. represent the return type of
  134. <code>operator()</code> given a call expression.</p>
  135. <p>Additionally, <code>boost::result_of</code>
  136. provides a third mode of operation, which some users
  137. may find convenient. When
  138. <code>BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK</code>
  139. is defined, <code>boost::result_of</code> behaves as
  140. follows. If the function object has a member
  141. type <code>result_type</code> or member
  142. template <code>result&lt;&gt;</code>, then
  143. <code>boost::result_of</code> will use the TR1
  144. protocol. Otherwise,
  145. <code>boost::result_of</code> will
  146. use <code>decltype</code>. Using TR1 with
  147. a <code>declytpe</code> fallback may workaround
  148. certain problems at the cost of portability. For
  149. example:
  150. <ul>
  151. <li>Deficient compiler: If your code
  152. requires <code>boost::result_of</code> to work
  153. with incomplete return types but your
  154. compiler's <code>decltype</code> implementation
  155. does not support incomplete return types, then you
  156. can use the TR1 protocol as a workaround. Support
  157. for incomplete return types was added late in the
  158. C++11 standardization process
  159. (see <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3276.pdf">N3276</a>)
  160. and is not implemented by some compilers.</li>
  161. <li>Deficient legacy code: If your existing TR1
  162. function object advertises a different type than
  163. the actual result type deduced
  164. by <code>decltype</code>, then using TR1 with a
  165. <code>decltype</code> fallback will allow you to
  166. work with both your existing TR1 function objects
  167. and new C++11 function object. This situation
  168. could occur if your legacy function objects
  169. misused the TR1 protocol. See the documentation on
  170. known <a href="#result_of_tr1_diff">differences</a>
  171. between <code>boost::result_of</code> and TR1.</li>
  172. </ul>
  173. <a name="BOOST_NO_RESULT_OF"></a>
  174. <p>This implementation of <code>result_of</code>
  175. requires class template partial specialization, the
  176. ability to parse function types properly, and support
  177. for SFINAE. If <code>result_of</code> is not supported
  178. by your compiler, including the header
  179. <code>boost/utility/result_of.hpp</code> will
  180. define the macro <code>BOOST_NO_RESULT_OF</code>.</p>
  181. <p>For additional information
  182. about <code>result_of</code>, see the C++ Library
  183. Technical Report,
  184. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf">N1836</a>,
  185. or, for motivation and design rationale,
  186. the <code>result_of</code> <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1454.html">proposal</a>.</p>
  187. <a name="result_of_guidelines">
  188. <h3>Usage guidelines for boost::result_of</h3>
  189. </a>
  190. <p>The following are general suggestions about when
  191. and how to use <code>boost::result_of</code>.</p>
  192. <ol>
  193. <li> If you are targeting C++11 and are not concerned
  194. about portability to non-compliant compilers or
  195. previous versions of the standard, then use
  196. <code>std::result_of</code>. If <code>std::result_of</code>
  197. meets your needs, then there's no reason to stop using
  198. it.</li>
  199. <li> If you are targeting C++11 but may port your code
  200. to legacy compilers at some time in the future, then
  201. use <code>boost::result_of</code> with
  202. <code>decltype</code>. When <code>decltype</code> is
  203. used <code>boost::result_of</code>
  204. and <code>std::result_of</code> are usually
  205. interchangeable. See the documentation on
  206. known <a href="#result_of_cxx11_diff">differences</a>
  207. between boost::result_of and C++11 result_of.</li>
  208. <li> If compiler portability is required,
  209. use <code>boost::result_of</code> with the TR1 protocol.</li>
  210. </ol>
  211. <p>Regardless of how you
  212. configure <code>boost::result_of</code>, it is
  213. important to bear in mind that the return type of a
  214. function may change depending on its arguments, and
  215. additionally, the return type of a member function may
  216. change depending on the cv-qualification of the
  217. object. <code>boost::result_of</code> must be passed
  218. the appropriately cv-qualified types in order to
  219. deduce the corresponding return type. For example:
  220. <blockquote>
  221. <pre>struct functor {
  222. int& operator()(int);
  223. int const& operator()(int) const;
  224. float& operator()(float&);
  225. float const& operator()(float const&);
  226. };
  227. typedef boost::result_of&lt;
  228. functor(int)
  229. &gt;::type type1; // type1 is int &
  230. typedef boost::result_of&lt;
  231. const functor(int)
  232. &gt;::type type2; // type2 is int const &
  233. typedef boost::result_of&lt;
  234. functor(float&)
  235. &gt;::type type3; // type3 is float &
  236. typedef boost::result_of&lt;
  237. functor(float const&)
  238. &gt;::type type4; // type4 is float const &</pre>
  239. </blockquote>
  240. <a name="result_of_tr1_protocol_guidelines">
  241. <h3>Usage guidelines for the TR1 result_of protocol</h3>
  242. </a>
  243. <p>On compliant C++11
  244. compilers, <code>boost::result_of</code> can
  245. use <code>decltype</code> to deduce the type of any
  246. call expression, including calls to function
  247. objects. However, on pre-C++11 compilers or on
  248. compilers without adequate decltype support,
  249. additional scaffolding is needed from function
  250. objects as described above. The following are
  251. suggestions about how to use the TR1 protocol.</p>
  252. <ul>
  253. <li>When the return type does not depend on the
  254. argument types or the cv-qualification of the
  255. function object, simply
  256. define <code>result_type</code>. There is no need
  257. to use the <code>result</code> template unless the
  258. return type varies.</li>
  259. <li>Use the protocol specified type when defining
  260. function prototypes. This can help ensure the
  261. actual return type does not get out of sync with
  262. the protocol specification. For example:
  263. <blockquote>
  264. <pre>struct functor {
  265. typedef int result_type;
  266. result_type operator()(int);
  267. };</pre>
  268. </blockquote> </li>
  269. <li>Always specify the <code>result</code>
  270. specialization near the corresponding
  271. <code>operator()</code> overload. This can make it
  272. easier to keep the specializations in sync with the
  273. overloads. For example:
  274. <blockquote>
  275. <pre>struct functor {
  276. template&lt;class&gt; struct result;
  277. template&lt;class F&gt;
  278. struct result&lt;F(int)&gt; {
  279. typedef int& type;
  280. };
  281. result&lt;functor(int)&gt;::type operator()(int);
  282. template&lt;class F&gt;
  283. struct result&lt;const F(int)&gt; {
  284. typedef int const& type;
  285. };
  286. result&lt;const functor(int)&gt;::type operator()(int) const;
  287. };</pre>
  288. </blockquote> </li>
  289. <li>Use type transformations to simplify
  290. the <code>result</code> template specialization. For
  291. example, the following uses
  292. <a href="../type_traits/doc/html/index.html">Boost.TypeTraits</a>
  293. to specialize the <code>result</code> template for
  294. a single <code>operator()</code> that can be called on
  295. both a const and non-const function object with
  296. either an lvalue or rvalue argument.
  297. <blockquote>
  298. <pre>struct functor {
  299. template&lt;class&gt; struct result;
  300. template&lt;class F, class T&gt;
  301. struct result&lt;F(T)&gt;
  302. : boost::remove_cv&lt;
  303. typename boost::remove_reference&lt;T&gt;::type
  304. &gt;
  305. {};
  306. template&lt;class T&gt;
  307. T operator()(T const&amp; x) const;
  308. };</pre>
  309. </blockquote></li>
  310. </ul>
  311. <a name="result_of_tr1_diff">
  312. <h3>Known differences between boost::result_of and TR1 result_of</h3>
  313. </a>
  314. When using <code>decltype</code>, <code>boost::result_of</code>
  315. ignores the TR1 protocol and instead deduces the
  316. return type of function objects directly
  317. via <code>decltype</code>. In most situations, users
  318. will not notice a difference, so long as they use the
  319. protocol correctly. The following are situations in
  320. which the type deduced
  321. by <code>boost::result_of</code> is known to differ depending on
  322. whether <code>decltype</code> or the TR1 protocol is
  323. used.
  324. <ul>
  325. <li> TR1 protocol misusage
  326. <p>When using the TR1
  327. protocol, <code>boost::result_of</code> cannot
  328. detect whether the actual type of a call to a
  329. function object is the same as the type specified
  330. by the protocol, which allows for the possibility
  331. of inadvertent mismatches between the specified
  332. type and the actual type. When
  333. using <code>decltype</code>, these subtle bugs
  334. may result in compilation errors. For example:</p>
  335. <blockquote>
  336. <pre>struct functor {
  337. typedef short result_type;
  338. int operator()(short);
  339. };
  340. #ifdef BOOST_RESULT_OF_USE_DECLTYPE
  341. BOOST_STATIC_ASSERT((
  342. boost::is_same&lt;boost::result_of&lt;functor(short)&gt;::type, int&gt;::value
  343. ));
  344. #else
  345. BOOST_STATIC_ASSERT((
  346. boost::is_same&lt;boost::result_of&lt;functor(short)&gt;::type, short&gt;::value
  347. ));
  348. #endif</pre>
  349. </blockquote>
  350. <p>Note that the user can
  351. force <code>boost::result_of</code> to use the TR1
  352. protocol even on platforms that
  353. support <code>decltype</code> by
  354. defining <code>BOOST_RESULT_OF_USE_TR1</code>.</p></li>
  355. <li> Nullary function objects
  356. <p>When using the TR1 protocol, <code>boost::result_of</code>
  357. cannot always deduce the type of calls to
  358. nullary function objects, in which case the
  359. type defaults to void. When using <code>decltype</code>,
  360. <code>boost::result_of</code> always gives the actual type of the
  361. call expression. For example:</p>
  362. <blockquote>
  363. <pre>struct functor {
  364. template&lt;class&gt; struct result {
  365. typedef int type;
  366. };
  367. int operator()();
  368. };
  369. #ifdef BOOST_RESULT_OF_USE_DECLTYPE
  370. BOOST_STATIC_ASSERT((
  371. boost::is_same&lt;boost::result_of&lt;functor()&gt;::type, int&gt;::value
  372. ));
  373. #else
  374. BOOST_STATIC_ASSERT((
  375. boost::is_same&lt;boost::result_of&lt;functor()&gt;::type, void&gt;::value
  376. ));
  377. #endif</pre>
  378. </blockquote>
  379. <p>Note that there are some workarounds for the
  380. nullary function problem. So long as the return
  381. type does not vary,
  382. <code>result_type</code> can always be used to
  383. specify the return type regardless of arity. If the
  384. return type does vary, then the user can
  385. specialize <code>boost::result_of</code> itself for
  386. nullary calls.</p></li>
  387. <li> Non-class prvalues and cv-qualification
  388. <p>When using the TR1
  389. protocol, <code>boost::result_of</code> will
  390. report the cv-qualified type specified
  391. by <code>result_type</code> or
  392. the <code>result</code> template regardless of
  393. the actual cv-qualification of the call
  394. expression. When using
  395. <code>decltype</code>, <code>boost::result_of</code>
  396. will report the actual type of the call expression,
  397. which is not cv-qualified when the expression is a
  398. non-class prvalue. For example:</p>
  399. <blockquote>
  400. <pre>struct functor {
  401. template&lt;class&gt; struct result;
  402. template&lt;class F, class T&gt; struct result&lt;F(const T)&gt; {
  403. typedef const T type;
  404. };
  405. const short operator()(const short);
  406. int const & operator()(int const &);
  407. };
  408. // Non-prvalue call expressions work the same with or without decltype.
  409. BOOST_STATIC_ASSERT((
  410. boost::is_same&lt;
  411. boost::result_of&lt;functor(int const &)&gt;::type,
  412. int const &
  413. ::value
  414. ));
  415. // Non-class prvalue call expressions are not actually cv-qualified,
  416. // but only the decltype-based result_of reports this accurately.
  417. #ifdef BOOST_RESULT_OF_USE_DECLTYPE
  418. BOOST_STATIC_ASSERT((
  419. boost::is_same&lt;
  420. boost::result_of&lt;functor(const short)&gt;::type,
  421. short
  422. ::value
  423. ));
  424. #else
  425. BOOST_STATIC_ASSERT((
  426. boost::is_same&lt;
  427. boost::result_of&lt;functor(const short)&gt;::type,
  428. const short
  429. ::value
  430. ));
  431. #endif</pre>
  432. </blockquote></li>
  433. </ul>
  434. <a name="result_of_cxx11_diff">
  435. <h3>Known differences between boost::result_of and C++11 result_of</h3>
  436. </a>
  437. <p>When using <code>decltype</code>, <code>boost::result_of</code>
  438. implements most of the C++11 result_of
  439. specification. One known exception is that
  440. <code>boost::result_of</code> does not implement the
  441. requirements regarding pointers to member data.</p>
  442. <p>Created by Doug Gregor. Contributions from Daniel Walker, Eric Niebler, Michel Morin and others</p>
  443. <h2><a name="BOOST_BINARY">Macro BOOST_BINARY</a></h2>
  444. <p>The macro <code>BOOST_BINARY</code> is used for the
  445. representation of binary literals. It takes as an argument
  446. a binary number arranged as an arbitrary amount of 1s and 0s in
  447. groupings of length 1 to 8, with groups separated
  448. by spaces. The type of the literal yielded is determined by
  449. the same rules as those of hex and octal
  450. literals (<i>2.13.1p1</i>). By implementation, this macro
  451. expands directly to an octal literal during preprocessing, so
  452. there is no overhead at runtime and the result is useable in
  453. any place that an octal literal would be.</p>
  454. <p>In order to directly support binary literals with suffixes,
  455. additional macros of the form BOOST_BINARY_XXX are also
  456. provided, where XXX is a standard integer suffix in all capital
  457. letters. In addition, LL and ULL suffixes may be used for representing
  458. long long and unsigned long long types in compilers which provide
  459. them as an extension.</p>
  460. <p>The BOOST_BINARY family of macros resides in the header
  461. <a
  462. href="../../boost/utility/binary.hpp">&lt;boost/utility/binary.hpp&gt;</a>
  463. which is automatically included by
  464. <a
  465. href="../../boost/utility.hpp">&lt;boost/utility.hpp&gt;</a>.
  466. <p>Contributed by Matt Calabrese.</p><p>
  467. </p><h3>Example</h3>
  468. <blockquote>
  469. <pre>
  470. void foo( int );
  471. void foo( unsigned long );
  472. void bar()
  473. {
  474. int value1 = BOOST_BINARY( 100 111000 01 1 110 );
  475. unsigned long value2 = BOOST_BINARY_UL( 100 001 ); // unsigned long
  476. long long value3 = BOOST_BINARY_LL( 11 000 ); // long long if supported
  477. assert( BOOST_BINARY( 10010 )
  478. & BOOST_BINARY( 11000 )
  479. == BOOST_BINARY( 10000 )
  480. );
  481. foo( BOOST_BINARY( 1010 ) ); // calls the first foo
  482. foo( BOOST_BINARY_LU( 1010 ) ); // calls the second foo
  483. }
  484. </pre></blockquote>
  485. <hr>
  486. <p>Revised&nbsp; <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan
  487. -->04 September, 2008<!--webbot bot="Timestamp" endspan i-checksum="39369"
  488. -->
  489. </p>
  490. <p>&copy; Copyright Beman Dawes 1999-2003.</p>
  491. <p>Distributed under the Boost Software License, Version 1.0. See
  492. <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
  493. </body>
  494. </html>