call_traits.htm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type"
  4. content="text/html; charset=iso-8859-1">
  5. <meta name="Template"
  6. content="C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE\html.dot">
  7. <meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
  8. <title>Call Traits</title>
  9. </head>
  10. <body bgcolor="#FFFFFF" text="#000000" link="#0000FF"
  11. vlink="#800080">
  12. <h1><img src="../../boost.png" width="276" height="86">Header
  13. &lt;<a href="../../boost/detail/call_traits.hpp">boost/call_traits.hpp</a>&gt;</h1>
  14. <p>All of the contents of &lt;boost/call_traits.hpp&gt; are
  15. defined inside namespace boost.</p>
  16. <p>The template class call_traits&lt;T&gt; encapsulates the
  17. &quot;best&quot; method to pass a parameter of some type T to or
  18. from a function, and consists of a collection of typedefs defined
  19. as in the table below. The purpose of call_traits is to ensure
  20. that problems like &quot;<a href="#refs">references to references</a>&quot;
  21. never occur, and that parameters are passed in the most efficient
  22. manner possible (see <a href="#examples">examples</a>). In each
  23. case if your existing practice is to use the type defined on the
  24. left, then replace it with the call_traits defined type on the
  25. right. </p>
  26. <p>Note that for compilers that do not support either partial
  27. specialization or member templates, no benefit will occur from
  28. using call_traits: the call_traits defined types will always be
  29. the same as the existing practice in this case. In addition if
  30. only member templates and not partial template specialisation is
  31. support by the compiler (for example Visual C++ 6) then
  32. call_traits can not be used with array types (although it can be
  33. used to solve the reference to reference problem).</p>
  34. <table border="0" cellpadding="7" cellspacing="1" width="797">
  35. <tr>
  36. <td valign="top" width="17%" bgcolor="#008080"><p
  37. align="center">Existing practice</p>
  38. </td>
  39. <td valign="top" width="35%" bgcolor="#008080"><p
  40. align="center">call_traits equivalent</p>
  41. </td>
  42. <td valign="top" width="32%" bgcolor="#008080"><p
  43. align="center">Description</p>
  44. </td>
  45. <td valign="top" width="16%" bgcolor="#008080"><p
  46. align="center">Notes</p>
  47. </td>
  48. </tr>
  49. <tr>
  50. <td valign="top" width="17%"><p align="center">T<br>
  51. (return by value)</p>
  52. </td>
  53. <td valign="top" width="35%"><p align="center"><code>call_traits&lt;T&gt;::value_type</code></p>
  54. </td>
  55. <td valign="top" width="32%">Defines a type that
  56. represents the &quot;value&quot; of type T. Use this for
  57. functions that return by value, or possibly for stored
  58. values of type T.</td>
  59. <td valign="top" width="16%"><p align="center">2</p>
  60. </td>
  61. </tr>
  62. <tr>
  63. <td valign="top" width="17%"><p align="center">T&amp;<br>
  64. (return value)</p>
  65. </td>
  66. <td valign="top" width="35%"><p align="center"><code>call_traits&lt;T&gt;::reference</code></p>
  67. </td>
  68. <td valign="top" width="32%">Defines a type that
  69. represents a reference to type T. Use for functions that
  70. would normally return a T&amp;.</td>
  71. <td valign="top" width="16%"><p align="center">1</p>
  72. </td>
  73. </tr>
  74. <tr>
  75. <td valign="top" width="17%"><p align="center">const
  76. T&amp;<br>
  77. (return value)</p>
  78. </td>
  79. <td valign="top" width="35%"><p align="center"><code>call_traits&lt;T&gt;::const_reference</code></p>
  80. </td>
  81. <td valign="top" width="32%">Defines a type that
  82. represents a constant reference to type T. Use for
  83. functions that would normally return a const T&amp;.</td>
  84. <td valign="top" width="16%"><p align="center">1</p>
  85. </td>
  86. </tr>
  87. <tr>
  88. <td valign="top" width="17%"><p align="center">const
  89. T&amp;<br>
  90. (function parameter)</p>
  91. </td>
  92. <td valign="top" width="35%"><p align="center"><code>call_traits&lt;T&gt;::param_type</code></p>
  93. </td>
  94. <td valign="top" width="32%">Defines a type that
  95. represents the &quot;best&quot; way to pass a parameter
  96. of type T to a function.</td>
  97. <td valign="top" width="16%"><p align="center">1,3</p>
  98. </td>
  99. </tr>
  100. </table>
  101. <p>Notes:</p>
  102. <ol>
  103. <li>If T is already reference type, then call_traits is
  104. defined such that <a href="#refs">references to
  105. references</a> do not occur (requires partial
  106. specialization).</li>
  107. <li>If T is an array type, then call_traits defines <code>value_type</code>
  108. as a &quot;constant pointer to type&quot; rather than an
  109. &quot;array of type&quot; (requires partial
  110. specialization). Note that if you are using value_type as
  111. a stored value then this will result in storing a &quot;constant
  112. pointer to an array&quot; rather than the array itself.
  113. This may or may not be a good thing depending upon what
  114. you actually need (in other words take care!).</li>
  115. <li>If T is a small built in type or a pointer, then <code>param_type</code>
  116. is defined as <code>T const</code>, instead of <code>T
  117. const&amp;</code>. This can improve the ability of the
  118. compiler to optimize loops in the body of the function if
  119. they depend upon the passed parameter, the semantics of
  120. the passed parameter is otherwise unchanged (requires
  121. partial specialization).</li>
  122. </ol>
  123. <p>&nbsp;</p>
  124. <h3>Copy constructibility</h3>
  125. <p>The following table defines which call_traits types can always
  126. be copy-constructed from which other types, those entries marked
  127. with a '?' are true only if and only if T is copy constructible:</p>
  128. <table border="0" cellpadding="7" cellspacing="1" width="766">
  129. <tr>
  130. <td valign="top" width="17%">&nbsp;</td>
  131. <td valign="top" colspan="5" width="85%"
  132. bgcolor="#008080"><p align="center">To:</p>
  133. </td>
  134. </tr>
  135. <tr>
  136. <td valign="top" width="17%" bgcolor="#008080">From:</td>
  137. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  138. align="center">T</p>
  139. </td>
  140. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  141. align="center">value_type</p>
  142. </td>
  143. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  144. align="center">reference</p>
  145. </td>
  146. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  147. align="center">const_reference</p>
  148. </td>
  149. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  150. align="center">param_type</p>
  151. </td>
  152. </tr>
  153. <tr>
  154. <td valign="top" width="17%" bgcolor="#C0C0C0">T</td>
  155. <td valign="top" width="17%"><p align="center">?</p>
  156. </td>
  157. <td valign="top" width="17%"><p align="center">?</p>
  158. </td>
  159. <td valign="top" width="17%"><p align="center">Y</p>
  160. </td>
  161. <td valign="top" width="17%"><p align="center">Y</p>
  162. </td>
  163. <td valign="top" width="17%"><p align="center">Y</p>
  164. </td>
  165. </tr>
  166. <tr>
  167. <td valign="top" width="17%" bgcolor="#C0C0C0">value_type</td>
  168. <td valign="top" width="17%"><p align="center">?</p>
  169. </td>
  170. <td valign="top" width="17%"><p align="center">?</p>
  171. </td>
  172. <td valign="top" width="17%"><p align="center">N</p>
  173. </td>
  174. <td valign="top" width="17%"><p align="center">N</p>
  175. </td>
  176. <td valign="top" width="17%"><p align="center">Y</p>
  177. </td>
  178. </tr>
  179. <tr>
  180. <td valign="top" width="17%" bgcolor="#C0C0C0">reference</td>
  181. <td valign="top" width="17%"><p align="center">?</p>
  182. </td>
  183. <td valign="top" width="17%"><p align="center">?</p>
  184. </td>
  185. <td valign="top" width="17%"><p align="center">Y</p>
  186. </td>
  187. <td valign="top" width="17%"><p align="center">Y</p>
  188. </td>
  189. <td valign="top" width="17%"><p align="center">Y</p>
  190. </td>
  191. </tr>
  192. <tr>
  193. <td valign="top" width="17%" bgcolor="#C0C0C0">const_reference</td>
  194. <td valign="top" width="17%"><p align="center">?</p>
  195. </td>
  196. <td valign="top" width="17%"><p align="center">N</p>
  197. </td>
  198. <td valign="top" width="17%"><p align="center">N</p>
  199. </td>
  200. <td valign="top" width="17%"><p align="center">Y</p>
  201. </td>
  202. <td valign="top" width="17%"><p align="center">Y</p>
  203. </td>
  204. </tr>
  205. <tr>
  206. <td valign="top" width="17%" bgcolor="#C0C0C0">param_type</td>
  207. <td valign="top" width="17%"><p align="center">?</p>
  208. </td>
  209. <td valign="top" width="17%"><p align="center">?</p>
  210. </td>
  211. <td valign="top" width="17%"><p align="center">N</p>
  212. </td>
  213. <td valign="top" width="17%"><p align="center">N</p>
  214. </td>
  215. <td valign="top" width="17%"><p align="center">Y</p>
  216. </td>
  217. </tr>
  218. </table>
  219. <p>&nbsp;</p>
  220. <p>If T is an assignable type the following assignments are
  221. possible:</p>
  222. <table border="0" cellpadding="7" cellspacing="1" width="766">
  223. <tr>
  224. <td valign="top" width="17%">&nbsp;</td>
  225. <td valign="top" colspan="5" width="85%"
  226. bgcolor="#008080"><p align="center">To:</p>
  227. </td>
  228. </tr>
  229. <tr>
  230. <td valign="top" width="17%" bgcolor="#008080">From:</td>
  231. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  232. align="center">T</p>
  233. </td>
  234. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  235. align="center">value_type</p>
  236. </td>
  237. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  238. align="center">reference</p>
  239. </td>
  240. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  241. align="center">const_reference</p>
  242. </td>
  243. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  244. align="center">param_type</p>
  245. </td>
  246. </tr>
  247. <tr>
  248. <td valign="top" width="17%" bgcolor="#C0C0C0">T</td>
  249. <td valign="top" width="17%"><p align="center">Y</p>
  250. </td>
  251. <td valign="top" width="17%"><p align="center">Y</p>
  252. </td>
  253. <td valign="top" width="17%"><p align="center">-</p>
  254. </td>
  255. <td valign="top" width="17%"><p align="center">-</p>
  256. </td>
  257. <td valign="top" width="17%"><p align="center">-</p>
  258. </td>
  259. </tr>
  260. <tr>
  261. <td valign="top" width="17%" bgcolor="#C0C0C0">value_type</td>
  262. <td valign="top" width="17%"><p align="center">Y</p>
  263. </td>
  264. <td valign="top" width="17%"><p align="center">Y</p>
  265. </td>
  266. <td valign="top" width="17%"><p align="center">-</p>
  267. </td>
  268. <td valign="top" width="17%"><p align="center">-</p>
  269. </td>
  270. <td valign="top" width="17%"><p align="center">-</p>
  271. </td>
  272. </tr>
  273. <tr>
  274. <td valign="top" width="17%" bgcolor="#C0C0C0">reference</td>
  275. <td valign="top" width="17%"><p align="center">Y</p>
  276. </td>
  277. <td valign="top" width="17%"><p align="center">Y</p>
  278. </td>
  279. <td valign="top" width="17%"><p align="center">-</p>
  280. </td>
  281. <td valign="top" width="17%"><p align="center">-</p>
  282. </td>
  283. <td valign="top" width="17%"><p align="center">-</p>
  284. </td>
  285. </tr>
  286. <tr>
  287. <td valign="top" width="17%" bgcolor="#C0C0C0">const_reference</td>
  288. <td valign="top" width="17%"><p align="center">Y</p>
  289. </td>
  290. <td valign="top" width="17%"><p align="center">Y</p>
  291. </td>
  292. <td valign="top" width="17%"><p align="center">-</p>
  293. </td>
  294. <td valign="top" width="17%"><p align="center">-</p>
  295. </td>
  296. <td valign="top" width="17%"><p align="center">-</p>
  297. </td>
  298. </tr>
  299. <tr>
  300. <td valign="top" width="17%" bgcolor="#C0C0C0">param_type</td>
  301. <td valign="top" width="17%"><p align="center">Y</p>
  302. </td>
  303. <td valign="top" width="17%"><p align="center">Y</p>
  304. </td>
  305. <td valign="top" width="17%"><p align="center">-</p>
  306. </td>
  307. <td valign="top" width="17%"><p align="center">-</p>
  308. </td>
  309. <td valign="top" width="17%"><p align="center">-</p>
  310. </td>
  311. </tr>
  312. </table>
  313. <p>&nbsp;</p>
  314. <h3><a name="examples"></a>Examples</h3>
  315. <p>The following table shows the effect that call_traits has on
  316. various types, the table assumes that the compiler supports
  317. partial specialization: if it doesn't then all types behave in
  318. the same way as the entry for &quot;myclass&quot;, and
  319. call_traits can not be used with reference or array types.</p>
  320. <table border="0" cellpadding="7" cellspacing="1" width="766">
  321. <tr>
  322. <td valign="top" width="17%">&nbsp;</td>
  323. <td valign="top" colspan="5" width="85%"
  324. bgcolor="#008080"><p align="center">Call_traits type:</p>
  325. </td>
  326. </tr>
  327. <tr>
  328. <td valign="top" width="17%" bgcolor="#008080"><p
  329. align="center">Original type T</p>
  330. </td>
  331. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  332. align="center">value_type</p>
  333. </td>
  334. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  335. align="center">reference</p>
  336. </td>
  337. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  338. align="center">const_reference</p>
  339. </td>
  340. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  341. align="center">param_type</p>
  342. </td>
  343. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  344. align="center">Applies to:</p>
  345. </td>
  346. </tr>
  347. <tr>
  348. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  349. align="center">myclass</p>
  350. </td>
  351. <td valign="top" width="17%"><p align="center">myclass</p>
  352. </td>
  353. <td valign="top" width="17%"><p align="center">myclass&amp;</p>
  354. </td>
  355. <td valign="top" width="17%"><p align="center">const
  356. myclass&amp;</p>
  357. </td>
  358. <td valign="top" width="17%"><p align="center">myclass
  359. const&amp;</p>
  360. </td>
  361. <td valign="top" width="17%"><p align="center">All user
  362. defined types.</p>
  363. </td>
  364. </tr>
  365. <tr>
  366. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  367. align="center">int</p>
  368. </td>
  369. <td valign="top" width="17%"><p align="center">int</p>
  370. </td>
  371. <td valign="top" width="17%"><p align="center">int&amp;</p>
  372. </td>
  373. <td valign="top" width="17%"><p align="center">const
  374. int&amp;</p>
  375. </td>
  376. <td valign="top" width="17%"><p align="center">int const</p>
  377. </td>
  378. <td valign="top" width="17%"><p align="center">All small
  379. built-in types.</p>
  380. </td>
  381. </tr>
  382. <tr>
  383. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  384. align="center">int*</p>
  385. </td>
  386. <td valign="top" width="17%"><p align="center">int*</p>
  387. </td>
  388. <td valign="top" width="17%"><p align="center">int*&amp;</p>
  389. </td>
  390. <td valign="top" width="17%"><p align="center">int*const&amp;</p>
  391. </td>
  392. <td valign="top" width="17%"><p align="center">int* const</p>
  393. </td>
  394. <td valign="top" width="17%"><p align="center">All
  395. pointer types.</p>
  396. </td>
  397. </tr>
  398. <tr>
  399. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  400. align="center">int&amp;</p>
  401. </td>
  402. <td valign="top" width="17%"><p align="center">int&amp;</p>
  403. </td>
  404. <td valign="top" width="17%"><p align="center">int&amp;</p>
  405. </td>
  406. <td valign="top" width="17%"><p align="center">const
  407. int&amp;</p>
  408. </td>
  409. <td valign="top" width="17%"><p align="center">int&amp;</p>
  410. </td>
  411. <td valign="top" width="17%"><p align="center">All
  412. reference types.</p>
  413. </td>
  414. </tr>
  415. <tr>
  416. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  417. align="center">const int&amp;</p>
  418. </td>
  419. <td valign="top" width="17%"><p align="center">const
  420. int&amp;</p>
  421. </td>
  422. <td valign="top" width="17%"><p align="center">const
  423. int&amp;</p>
  424. </td>
  425. <td valign="top" width="17%"><p align="center">const
  426. int&amp;</p>
  427. </td>
  428. <td valign="top" width="17%"><p align="center">const
  429. int&amp;</p>
  430. </td>
  431. <td valign="top" width="17%"><p align="center">All
  432. constant-references.</p>
  433. </td>
  434. </tr>
  435. <tr>
  436. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  437. align="center">int[3]</p>
  438. </td>
  439. <td valign="top" width="17%"><p align="center">const int*</p>
  440. </td>
  441. <td valign="top" width="17%"><p align="center">int(&amp;)[3]</p>
  442. </td>
  443. <td valign="top" width="17%"><p align="center">const int(&amp;)[3]</p>
  444. </td>
  445. <td valign="top" width="17%"><p align="center">const int*
  446. const</p>
  447. </td>
  448. <td valign="top" width="17%"><p align="center">All array
  449. types.</p>
  450. </td>
  451. </tr>
  452. <tr>
  453. <td valign="top" width="17%" bgcolor="#C0C0C0"><p
  454. align="center">const int[3]</p>
  455. </td>
  456. <td valign="top" width="17%"><p align="center">const int*</p>
  457. </td>
  458. <td valign="top" width="17%"><p align="center">const int(&amp;)[3]</p>
  459. </td>
  460. <td valign="top" width="17%"><p align="center">const int(&amp;)[3]</p>
  461. </td>
  462. <td valign="top" width="17%"><p align="center">const int*
  463. const</p>
  464. </td>
  465. <td valign="top" width="17%"><p align="center">All
  466. constant-array types.</p>
  467. </td>
  468. </tr>
  469. </table>
  470. <p>&nbsp;</p>
  471. <h4>Example 1:</h4>
  472. <p>The following class is a trivial class that stores some type T
  473. by value (see the <a href="test/call_traits_test.cpp">call_traits_test.cpp</a>
  474. file), the aim is to illustrate how each of the available
  475. call_traits typedefs may be used:</p>
  476. <pre>template &lt;class T&gt;
  477. struct contained
  478. {
  479. // define our typedefs first, arrays are stored by value
  480. // so value_type is not the same as result_type:
  481. typedef typename boost::call_traits&lt;T&gt;::param_type param_type;
  482. typedef typename boost::call_traits&lt;T&gt;::reference reference;
  483. typedef typename boost::call_traits&lt;T&gt;::const_reference const_reference;
  484. typedef T value_type;
  485. typedef typename boost::call_traits&lt;T&gt;::value_type result_type;
  486. // stored value:
  487. value_type v_;
  488. // constructors:
  489. contained() {}
  490. contained(param_type p) : v_(p){}
  491. // return byval:
  492. result_type value() { return v_; }
  493. // return by_ref:
  494. reference get() { return v_; }
  495. const_reference const_get()const { return v_; }
  496. // pass value:
  497. void call(param_type p){}
  498. };</pre>
  499. <h4><a name="refs"></a>Example 2 (the reference to reference
  500. problem):</h4>
  501. <p>Consider the definition of std::binder1st:</p>
  502. <pre>template &lt;class Operation&gt;
  503. class binder1st :
  504. public unary_function&lt;typename Operation::second_argument_type, typename Operation::result_type&gt;
  505. {
  506. protected:
  507. Operation op;
  508. typename Operation::first_argument_type value;
  509. public:
  510. binder1st(const Operation&amp; x, const typename Operation::first_argument_type&amp; y);
  511. typename Operation::result_type operator()(const typename Operation::second_argument_type&amp; x) const;
  512. }; </pre>
  513. <p>Now consider what happens in the relatively common case that
  514. the functor takes its second argument as a reference, that
  515. implies that <code>Operation::second_argument_type</code> is a
  516. reference type, <code>operator()</code> will now end up taking a
  517. reference to a reference as an argument, and that is not
  518. currently legal. The solution here is to modify <code>operator()</code>
  519. to use call_traits:</p>
  520. <pre>typename Operation::result_type operator()(typename call_traits&lt;typename Operation::second_argument_type&gt;::param_type x) const;</pre>
  521. <p>Now in the case that <code>Operation::second_argument_type</code>
  522. is a reference type, the argument is passed as a reference, and
  523. the no &quot;reference to reference&quot; occurs.</p>
  524. <h4><a name="ex3"></a>Example 3 (the make_pair problem):</h4>
  525. <p>If we pass the name of an array as one (or both) arguments to <code>std::make_pair</code>,
  526. then template argument deduction deduces the passed parameter as
  527. &quot;const reference to array of T&quot;, this also applies to
  528. string literals (which are really array literals). Consequently
  529. instead of returning a pair of pointers, it tries to return a
  530. pair of arrays, and since an array type is not copy-constructible
  531. the code fails to compile. One solution is to explicitly cast the
  532. arguments to make_pair to pointers, but call_traits provides a
  533. better (i.e. automatic) solution (and one that works safely even
  534. in generic code where the cast might do the wrong thing):</p>
  535. <pre>template &lt;class T1, class T2&gt;
  536. std::pair&lt;
  537. typename boost::call_traits&lt;T1&gt;::value_type,
  538. typename boost::call_traits&lt;T2&gt;::value_type&gt;
  539. make_pair(const T1&amp; t1, const T2&amp; t2)
  540. {
  541. return std::pair&lt;
  542. typename boost::call_traits&lt;T1&gt;::value_type,
  543. typename boost::call_traits&lt;T2&gt;::value_type&gt;(t1, t2);
  544. }</pre>
  545. <p>Here, the deduced argument types will be automatically
  546. degraded to pointers if the deduced types are arrays, similar
  547. situations occur in the standard binders and adapters: in
  548. principle in any function that &quot;wraps&quot; a temporary
  549. whose type is deduced. Note that the function arguments to
  550. make_pair are not expressed in terms of call_traits: doing so
  551. would prevent template argument deduction from functioning.</p>
  552. <h4><a name="ex4"></a>Example 4 (optimising fill):</h4>
  553. <p>The call_traits template will &quot;optimize&quot; the passing
  554. of a small built-in type as a function parameter, this mainly has
  555. an effect when the parameter is used within a loop body. In the
  556. following example (see <a
  557. href="../type_traits/examples/fill_example.cpp">fill_example.cpp</a>),
  558. a version of std::fill is optimized in two ways: if the type
  559. passed is a single byte built-in type then std::memset is used to
  560. effect the fill, otherwise a conventional C++ implemention is
  561. used, but with the passed parameter &quot;optimized&quot; using
  562. call_traits:</p>
  563. <pre>namespace detail{
  564. template &lt;bool opt&gt;
  565. struct filler
  566. {
  567. template &lt;typename I, typename T&gt;
  568. static void do_fill(I first, I last, typename boost::call_traits&lt;T&gt;::param_type val)
  569. {
  570. while(first != last)
  571. {
  572. *first = val;
  573. ++first;
  574. }
  575. }
  576. };
  577. template &lt;&gt;
  578. struct filler&lt;true&gt;
  579. {
  580. template &lt;typename I, typename T&gt;
  581. static void do_fill(I first, I last, T val)
  582. {
  583. memset(first, val, last-first);
  584. }
  585. };
  586. }
  587. template &lt;class I, class T&gt;
  588. inline void fill(I first, I last, const T&amp; val)
  589. {
  590. enum{ can_opt = boost::is_pointer&lt;I&gt;::value
  591. &amp;&amp; boost::is_arithmetic&lt;T&gt;::value
  592. &amp;&amp; (sizeof(T) == 1) };
  593. typedef detail::filler&lt;can_opt&gt; filler_t;
  594. filler_t::template do_fill&lt;I,T&gt;(first, last, val);
  595. }</pre>
  596. <p>Footnote: the reason that this is &quot;optimal&quot; for
  597. small built-in types is that with the value passed as &quot;T
  598. const&quot; instead of &quot;const T&amp;&quot; the compiler is
  599. able to tell both that the value is constant and that it is free
  600. of aliases. With this information the compiler is able to cache
  601. the passed value in a register, unroll the loop, or use
  602. explicitly parallel instructions: if any of these are supported.
  603. Exactly how much mileage you will get from this depends upon your
  604. compiler - we could really use some accurate benchmarking
  605. software as part of boost for cases like this.</p>
  606. <p>Note that the function arguments to fill are not expressed in
  607. terms of call_traits: doing so would prevent template argument
  608. deduction from functioning. Instead fill acts as a &quot;thin
  609. wrapper&quot; that is there to perform template argument
  610. deduction, the compiler will optimise away the call to fill all
  611. together, replacing it with the call to filler&lt;&gt;::do_fill,
  612. which does use call_traits.</p>
  613. <h3>Rationale</h3>
  614. <p>The following notes are intended to briefly describe the
  615. rational behind choices made in call_traits.</p>
  616. <p>All user-defined types follow &quot;existing practice&quot;
  617. and need no comment.</p>
  618. <p>Small built-in types (what the standard calls fundamental
  619. types [3.9.1]) differ from existing practice only in the <i>param_type</i>
  620. typedef. In this case passing &quot;T const&quot; is compatible
  621. with existing practice, but may improve performance in some cases
  622. (see <a href="#ex4">Example 4</a>), in any case this should never
  623. be any worse than existing practice.</p>
  624. <p>Pointers follow the same rational as small built-in types.</p>
  625. <p>For reference types the rational follows <a href="#refs">Example
  626. 2</a> - references to references are not allowed, so the
  627. call_traits members must be defined such that these problems do
  628. not occur. There is a proposal to modify the language such that
  629. &quot;a reference to a reference is a reference&quot; (issue #106,
  630. submitted by Bjarne Stroustrup), call_traits&lt;T&gt;::value_type
  631. and call_traits&lt;T&gt;::param_type both provide the same effect
  632. as that proposal, without the need for a language change (in
  633. other words it's a workaround).</p>
  634. <p>For array types, a function that takes an array as an argument
  635. will degrade the array type to a pointer type: this means that
  636. the type of the actual parameter is different from its declared
  637. type, something that can cause endless problems in template code
  638. that relies on the declared type of a parameter. For example:</p>
  639. <pre>template &lt;class T&gt;
  640. struct A
  641. {
  642. void foo(T t);
  643. };</pre>
  644. <p><font face="Times New Roman">In this case if we instantiate
  645. A&lt;int[2]&gt; then the declared type of the parameter passed to
  646. member function foo is int[2], but it's actual type is const int*,
  647. if we try to use the type T within the function body, then there
  648. is a strong likelyhood that our code will not compile:</font></p>
  649. <pre>template &lt;class T&gt;
  650. void A&lt;T&gt;::foo(T t)
  651. {
  652. T dup(t); // doesn't compile for case that T is an array.
  653. }</pre>
  654. <p>By using call_traits the degradation from array to pointer is
  655. explicit, and the type of the parameter is the same as it's
  656. declared type:</p>
  657. <pre>template &lt;class T&gt;
  658. struct A
  659. {
  660. void foo(typename call_traits&lt;T&gt;::value_type t);
  661. };
  662. template &lt;class T&gt;
  663. void A&lt;T&gt;::foo(typename call_traits&lt;T&gt;::value_type t)
  664. {
  665. typename call_traits&lt;T&gt;::value_type dup(t); // OK even if T is an array type.
  666. }</pre>
  667. <p>For value_type (return by value), again only a pointer may be
  668. returned, not a copy of the whole array, and again call_traits
  669. makes the degradation explicit. The value_type member is useful
  670. whenever an array must be explicitly degraded to a pointer - <a
  671. href="#ex3">Example 3</a> provides the test case (Footnote: the
  672. array specialisation for call_traits is the least well understood
  673. of all the call_traits specialisations, if the given semantics
  674. cause specific problems for you, or don't solve a particular
  675. array-related problem, then I would be interested to hear about
  676. it. Most people though will probably never need to use this
  677. specialisation).</p>
  678. <hr>
  679. <p>Revised 01 September 2000</p>
  680. <p>
  681. Copyright 2000 Steve Cleary, Beman Dawes, Howard
  682. Hinnant and John Maddock. <br/>
  683. Use, modification and distribution are subject to the
  684. Boost Software License, Version 1.0.
  685. (See accompanying file LICENSE_1_0.txt
  686. or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
  687. http://www.boost.org/LICENSE_1_0.txt
  688. </a>).
  689. </p>
  690. </body>
  691. </html>