vmd_identifier.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Identifiers</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&#160;1.&#160;The Variadic Macro Data Library 1.9">
  8. <link rel="up" href="../vmd_specific.html" title="Specific macros for working with data types">
  9. <link rel="prev" href="vmd_constraints.html" title="Macro constraints">
  10. <link rel="next" href="vmd_number.html" title="Numbers">
  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="vmd_constraints.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../vmd_specific.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="vmd_number.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="variadic_macro_data.vmd_specific.vmd_identifier"></a><a class="link" href="vmd_identifier.html" title="Identifiers">Identifiers</a>
  28. </h3></div></div></div>
  29. <p>
  30. An identifier in VMD is either of two lower-level preprocessor possibilities:
  31. </p>
  32. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  33. <li class="listitem">
  34. a preprocessing token 'identifier', which is essentially a sequence of
  35. alphanumeric characters and the underscore character with the first character
  36. not being a numeric character.
  37. </li>
  38. <li class="listitem">
  39. a preprocessing token 'pp-number' that is an integral literal token.
  40. </li>
  41. </ul></div>
  42. <p>
  43. Here are some examples:
  44. </p>
  45. <pre class="programlisting"><span class="identifier">SOME_NAME</span>
  46. <span class="identifier">_SOME_NAME</span>
  47. <span class="identifier">SOME_123_NAME</span>
  48. <span class="identifier">some_123_name</span>
  49. <span class="identifier">sOMe_123_NAmE</span>
  50. <span class="number">2367</span>
  51. <span class="number">43e11</span>
  52. <span class="number">0</span>
  53. <span class="number">22</span>
  54. <span class="number">654792</span>
  55. <span class="number">0x1256</span>
  56. </pre>
  57. <h5>
  58. <a name="variadic_macro_data.vmd_specific.vmd_identifier.h0"></a>
  59. <span class="phrase"><a name="variadic_macro_data.vmd_specific.vmd_identifier.problem_testing_any_identifier"></a></span><a class="link" href="vmd_identifier.html#variadic_macro_data.vmd_specific.vmd_identifier.problem_testing_any_identifier">Problem
  60. testing any identifier</a>
  61. </h5>
  62. <p>
  63. One of the difficulties with identifiers in preprocessor metaprogramming
  64. is safely testing for a particular one. VMD has a means of doing this within
  65. a particular constraint for the characters that serve as the input.
  66. </p>
  67. <p>
  68. The constraint is that the beginning input character, ignoring any whitespace,
  69. passed as the input to test must be either:
  70. </p>
  71. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  72. <li class="listitem">
  73. an identifier character, ie. an alphanumeric or an underscore
  74. </li>
  75. <li class="listitem">
  76. the left parenthesis of a tuple
  77. </li>
  78. </ul></div>
  79. <p>
  80. and if the first character is not the left parenthesis of a tuple the remaining
  81. characters must be alphanumeric or an underscore until a space character
  82. or end of input occurs.
  83. </p>
  84. <p>
  85. If this is not the case the behavior is undefined, and most likely a preprocessing
  86. error will occur.
  87. </p>
  88. <p>
  89. Given the input:
  90. </p>
  91. <pre class="programlisting"><span class="char">'s_anything'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span>
  92. <span class="char">'S_anything'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span>
  93. <span class="char">'s_anYthiNg'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span>
  94. <span class="char">'_anything'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span>
  95. <span class="char">'_Anything'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span>
  96. <span class="char">'_anytHIng'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span>
  97. <span class="char">'24'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span>
  98. <span class="char">'245e2'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span>
  99. <span class="char">'(anything)'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span><span class="special">,</span> <span class="identifier">tuple</span>
  100. <span class="char">'(anything) anything'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span><span class="special">,</span> <span class="identifier">tuple</span> <span class="keyword">and</span> <span class="identifier">further</span> <span class="identifier">input</span>
  101. <span class="char">'anything anything'</span> <span class="special">:</span> <span class="identifier">can</span> <span class="identifier">be</span> <span class="identifier">tested</span><span class="special">,</span> <span class="identifier">identifier</span> <span class="identifier">followed</span> <span class="identifier">by</span> <span class="identifier">space</span> <span class="identifier">character</span>
  102. <span class="char">'%_anything'</span> <span class="special">:</span> <span class="identifier">undefined</span> <span class="identifier">behavior</span> <span class="keyword">and</span> <span class="identifier">most</span> <span class="identifier">likely</span> <span class="identifier">a</span> <span class="identifier">preprocessing</span> <span class="identifier">error</span> <span class="identifier">due</span> <span class="identifier">to</span> <span class="identifier">the</span> <span class="identifier">constraint</span>
  103. <span class="char">'(_anything'</span> <span class="special">:</span> <span class="identifier">undefined</span> <span class="identifier">behavior</span> <span class="keyword">and</span> <span class="identifier">most</span> <span class="identifier">likely</span> <span class="identifier">a</span> <span class="identifier">preprocessing</span> <span class="identifier">error</span> <span class="identifier">due</span> <span class="identifier">to</span> <span class="identifier">the</span> <span class="identifier">constraint</span><span class="special">,</span> <span class="identifier">since</span> <span class="identifier">a</span> <span class="identifier">single</span> <span class="char">'('</span> <span class="identifier">does</span> <span class="keyword">not</span> <span class="identifier">form</span> <span class="identifier">a</span> <span class="identifier">tuple</span>
  104. <span class="char">'44.3'</span> <span class="special">:</span> <span class="identifier">undefined</span> <span class="identifier">behavior</span> <span class="keyword">and</span> <span class="identifier">most</span> <span class="identifier">likely</span> <span class="identifier">a</span> <span class="identifier">preprocessing</span> <span class="identifier">error</span> <span class="identifier">due</span> <span class="identifier">to</span> <span class="identifier">the</span> <span class="identifier">constraint</span> <span class="identifier">since</span> <span class="char">'.'</span> <span class="identifier">is</span> <span class="keyword">not</span> <span class="identifier">alphanumeric</span>
  105. </pre>
  106. <h5>
  107. <a name="variadic_macro_data.vmd_specific.vmd_identifier.h1"></a>
  108. <span class="phrase"><a name="variadic_macro_data.vmd_specific.vmd_identifier.identifying_an_identifier"></a></span><a class="link" href="vmd_identifier.html#variadic_macro_data.vmd_specific.vmd_identifier.identifying_an_identifier">Identifying
  109. an identifier</a>
  110. </h5>
  111. <p>
  112. In VMD the only way an identifier can be identified in preprocessor input
  113. is by a process called registration. In order to 'register' an identifier
  114. to be recognized by VMD the end-user must create, for every identifier to
  115. be recognized, an object-like macro whose form is:
  116. </p>
  117. <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_VMD_REGISTER_identifier</span> <span class="special">(</span><span class="identifier">identifier</span><span class="special">)</span>
  118. </pre>
  119. <p>
  120. where 'identifier' is a particular identifier we wish to identify. This is
  121. called in VMD a registration macro.
  122. </p>
  123. <p>
  124. It is recommended that such registration macros be created in a header file
  125. which can be included before the end-user uses the identifier macros of VMD.
  126. </p>
  127. <p>
  128. If a particular registration macro occurs more than once it is not a preprocessing
  129. error, so duplicating a registration macro will not lead to any problems
  130. since each registration macro of the same name will have the exact same object-like
  131. macro expansion.
  132. </p>
  133. <p>
  134. Within a given translation unit it could potentially happen that registration
  135. macros have been included by header files which a particular end-user of
  136. VMD has not created. This should also not lead to particular problems since
  137. registration is a process for adding identifiers for any particular translation
  138. unit. As we shall see VMD has macros for not only finding any identifier
  139. in preprocessor input but for also finding any particular identifier in preprocessor
  140. input.
  141. </p>
  142. <h5>
  143. <a name="variadic_macro_data.vmd_specific.vmd_identifier.h2"></a>
  144. <span class="phrase"><a name="variadic_macro_data.vmd_specific.vmd_identifier.testing_for_an_identifier_macro"></a></span><a class="link" href="vmd_identifier.html#variadic_macro_data.vmd_specific.vmd_identifier.testing_for_an_identifier_macro">Testing
  145. for an identifier macro</a>
  146. </h5>
  147. <p>
  148. The specific macro used to test for an identifier in VMD is called BOOST_VMD_IS_IDENTIFIER.
  149. The macro takes one required parameter which is the input against which to
  150. test.
  151. </p>
  152. <p>
  153. When we invoke BOOST_VMD_IS_IDENTIFIER it returns 1 if the input represents
  154. any registered identifier, otherwise it returns 0.
  155. </p>
  156. <p>
  157. As an example:
  158. </p>
  159. <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">vmd</span><span class="special">/</span><span class="identifier">is_identifier</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  160. <span class="preprocessor">#define</span> <span class="identifier">BOOST_VMD_REGISTER_yellow</span> <span class="special">(</span><span class="identifier">yellow</span><span class="special">)</span>
  161. <span class="preprocessor">#define</span> <span class="identifier">BOOST_VMD_REGISTER_green</span> <span class="special">(</span><span class="identifier">green</span><span class="special">)</span>
  162. <span class="preprocessor">#define</span> <span class="identifier">BOOST_VMD_REGISTER_blue</span> <span class="special">(</span><span class="identifier">blue</span><span class="special">)</span>
  163. <span class="identifier">BOOST_VMD_IS_IDENTIFIER</span><span class="special">(</span><span class="identifier">some_input</span><span class="special">)</span> <span class="comment">// returns 1 if 'some_input' is 'yellow','green', or 'blue'</span>
  164. <span class="identifier">BOOST_VMD_IS_IDENTIFIER</span><span class="special">(</span><span class="identifier">some_input</span><span class="special">)</span> <span class="comment">// returns 0 if 'some_input' is 'purple'</span>
  165. </pre>
  166. <p>
  167. Essentially only registered identifiers can be found in VMD as identifiers.
  168. </p>
  169. <h5>
  170. <a name="variadic_macro_data.vmd_specific.vmd_identifier.h3"></a>
  171. <span class="phrase"><a name="variadic_macro_data.vmd_specific.vmd_identifier.detecting_a_particular_identifie"></a></span><a class="link" href="vmd_identifier.html#variadic_macro_data.vmd_specific.vmd_identifier.detecting_a_particular_identifie">Detecting
  172. a particular identifier</a>
  173. </h5>
  174. <p>
  175. Although registering an identifier allows VMD to recognize the string of
  176. characters as a VMD identifier, the ability to detect a particular identifier
  177. needs the end-user to define another macro:
  178. </p>
  179. <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_VMD_DETECT_identifier_identifier</span>
  180. </pre>
  181. <p>
  182. where 'identifier' is a particular identifier we wish to detect. This object-like
  183. macro expands to no output.
  184. </p>
  185. <p>
  186. Like the registration macro multiple detection macros of the same identifier
  187. in a translation unit does not cause a compiler problem since the exact same
  188. object-like macro occurs.
  189. </p>
  190. <p>
  191. The term for creating this macro is that we have potentially 'pre-detected'
  192. the identifier and I will use the term pre-detected as the process of creating
  193. the BOOST_VMD_DETECT macro.
  194. </p>
  195. <p>
  196. The ability to detect that a VMD identifier is a particular identifier is
  197. used in VMD macros when data is compared for equality/inequality as well
  198. as when we want to match an identifier against a set of other identifiers.
  199. These situations will be explained later in the documentation when the particular
  200. macro functionality is discussed. If the programmer never uses the functionality
  201. which these situations encompass there is no need to use pre-detection for
  202. a registered identifier.
  203. </p>
  204. <h5>
  205. <a name="variadic_macro_data.vmd_specific.vmd_identifier.h4"></a>
  206. <span class="phrase"><a name="variadic_macro_data.vmd_specific.vmd_identifier.parsing_identifiers_and_undefine"></a></span><a class="link" href="vmd_identifier.html#variadic_macro_data.vmd_specific.vmd_identifier.parsing_identifiers_and_undefine">Parsing
  207. identifiers and undefined behavior</a>
  208. </h5>
  209. <p>
  210. The technique for parsing identifiers, once it is determined that the input
  211. being parsed does not begin with a set of parentheses, uses preprocessor
  212. concatenation in its parsing. This technique involves the preprocessor '##'
  213. operator to concatenate input, and examine the results of that concatenation.
  214. </p>
  215. <p>
  216. When preprocessor concatenation is used the result of the concatenation must
  217. be a valid preprocessing token, else the behavior of the preprocessor is
  218. undefined. In C++ 'undefined behavior' in general means that anything can
  219. happen. In practical use when preprocessor concatenation does not produce
  220. a valid preprocessing token, a compiler is most likely to generate a preprocessing
  221. error. If the compiler chooses not to issue a preprocessing error the outcome
  222. will always mean that parsing an identifier will fail. But because the outcome
  223. is undefined behavior there is no absolute way that the programmer can determine
  224. what the outcome will be when preprocessor concatenation is used and the
  225. input being parsed contains preprocessor input which does not meet the constraints
  226. for parsing an identifier mentioned at the beginning of this topic.
  227. </p>
  228. <p>
  229. In this documentation I will be using the abbreviation 'UB' as the shortened
  230. form of 'undefined behavior' to denote the particular occurrence where VMD
  231. attempts to parse preprocessor input using preprocessor concatenation and
  232. undefined behavior will occur.
  233. </p>
  234. <h5>
  235. <a name="variadic_macro_data.vmd_specific.vmd_identifier.h5"></a>
  236. <span class="phrase"><a name="variadic_macro_data.vmd_specific.vmd_identifier.usage"></a></span><a class="link" href="vmd_identifier.html#variadic_macro_data.vmd_specific.vmd_identifier.usage">Usage</a>
  237. </h5>
  238. <p>
  239. To use the BOOST_VMD_IS_IDENTIFIER macro either include the general header:
  240. </p>
  241. <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">vmd</span><span class="special">/</span><span class="identifier">vmd</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  242. </pre>
  243. <p>
  244. or include the specific header:
  245. </p>
  246. <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">vmd</span><span class="special">/</span><span class="identifier">is_identifier</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  247. </pre>
  248. </div>
  249. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  250. <td align="left"></td>
  251. <td align="right"><div class="copyright-footer">Copyright &#169; 2010-2017 Tropic Software
  252. East Inc</div></td>
  253. </tr></table>
  254. <hr>
  255. <div class="spirit-nav">
  256. <a accesskey="p" href="vmd_constraints.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../vmd_specific.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="vmd_number.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  257. </div>
  258. </body>
  259. </html>