vmd_constraints.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Macro constraints</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_specific.html" title="Specific macros for working with data types">
  10. <link rel="next" href="vmd_identifier.html" title="Identifiers">
  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_specific.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_identifier.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_constraints"></a><a class="link" href="vmd_constraints.html" title="Macro constraints">Macro
  28. constraints</a>
  29. </h3></div></div></div>
  30. <p>
  31. When discussing the BOOST_VMD_IS_EMPTY macro I mentioned constraining input
  32. to the macro. Now I will discuss what this means in terms of preprocessor
  33. metaprogramming and input to macros in general.
  34. </p>
  35. <h5>
  36. <a name="variadic_macro_data.vmd_specific.vmd_constraints.h0"></a>
  37. <span class="phrase"><a name="variadic_macro_data.vmd_specific.vmd_constraints.constrained_input"></a></span><a class="link" href="vmd_constraints.html#variadic_macro_data.vmd_specific.vmd_constraints.constrained_input">Constrained
  38. input</a>
  39. </h5>
  40. <p>
  41. When a programmer designs any kinds of callables in C++ ( functions, member
  42. functions etc. ), he specifies what the types of input and the return value
  43. are. The C++ compiler enforces this specification at compile time. Similarly
  44. at run-time a callable may check that its input falls within certain documented
  45. and defined boundaries and react accordingly if it does not. This is all
  46. part of the constraints for any callable in C++ and should be documented
  47. by any good programmer.
  48. </p>
  49. <p>
  50. The C++ preprocessor is much "dumber" than the C++ compiler and
  51. even with the preprocessor metaprogramming constructs which Paul Mensonides
  52. has created in Boost PP there is far less the preprocessor metaprogrammer
  53. can do at preprocessing time to constrain argument input to a macro than
  54. a programmer can do at compile-time and/or at run-time to constrain argument
  55. input to a C++ callable. Nevertheless it is perfectly valid to document what
  56. a macro expects as its argument input and, if a programmer does not follow
  57. the constraint, the macro will fail to work properly. In the ideal case in
  58. preprocessor metaprogramming the macro could tell whether or not the constraint
  59. was met and could issue some sort of intelligible preprocessing error when
  60. this occurred, but even within the reality of preprocessor metaprogramming
  61. with Boost PP this is not always possible to do. Nevertheless if the user
  62. of a macro does not follow the constraints for a macro parameter, as specified
  63. in the documentation of a particular macro being invoked, any error which
  64. occurs is the fault of that user. I realize that this may go against the
  65. strongly held concept that programming errors must always be met with some
  66. sort of compile-time or run-time occurrence which allows the programmer to
  67. correct the error, rather than a silent failure which masks the error. Because
  68. the preprocessor is "dumber" and cannot provide this occurrence
  69. in all cases the error could unfortunately be masked, despite the fact that
  70. the documentation specifies the correct input constraint(s). In the case
  71. of the already discussed macro BOOST_VMD_IS_EMPTY, this masking of the error
  72. could only occur with a preprocessor ( Visual C++ ) which is not C++ standard
  73. conformant.
  74. </p>
  75. <p>
  76. The Boost PP library does have a way of generating a preprocessing error,
  77. without generating preprocessor output, but once again this way does not
  78. work with the non-conformant preprocessor of Visual C++. The means to do
  79. so using Boost PP is through the BOOST_PP_ASSERT macro. As will be seen and
  80. discussed later VMD has an equivalent macro which will work with Visual C++
  81. by producing incorrect C++ output rather than a preprocessing error, but
  82. even this is not a complete solution since the incorrect C++ output produced
  83. could be hidden.
  84. </p>
  85. <p>
  86. Even the effort to produce a preprocessing error, or incorrect output inducing
  87. a compile-time error, does not solve the problem of constrained input for
  88. preprocessor metaprogramming. Often it is impossible to determine if the
  89. input meets the constraints which the preprocessor metaprogrammer places
  90. on it and documents. Certain preprocessing tokens cannot be checked reliably
  91. for particular values, or a range of values, without the checking mechanism
  92. itself creating a preprocessing error or undefined behavior.
  93. </p>
  94. <p>
  95. This does not mean that one should give up attempting to check macro input
  96. constraints. If it can be done I see the value of such checks and a number
  97. of VMD macros, discussed later, are designed as preprocessing input constraint
  98. checking macros. But the most important thing when dealing with macro input
  99. constraints is that they should be carefully documented, and that the programmer
  100. should know that if the constraints are not met either preprocessing errors
  101. or incorrect macro results could be the results.
  102. </p>
  103. <p>
  104. The VMD library, in order to present more preprocessor programming functionality
  105. and flexibility, allows that erroneous results could occur if certain input
  106. constraints are not met, whether the erroneous results are preprocessing
  107. errors or incorrect output from a VMD macro. At the same time the VMD does
  108. everything that the preprocessor is capable of doing to check the input constraints,
  109. and carefully documents for each macro in the library what the input for
  110. each could be in order to avoid erroneous output.
  111. </p>
  112. <p>
  113. Documented macro input constraints are just as valid in the preprocessor
  114. as compile-time/run-time constraints are valid in C++, even if the detection
  115. of such constraints and/or the handling of constraints that are not met are
  116. far more difficult, if not impossible, in the preprocessor than in the compile-time/run-time
  117. processing of C++.
  118. </p>
  119. <p>
  120. The VMD library uses constraints for most of it macros and the documentation
  121. for those macros mentions the constraints that apply in order to use the
  122. macro.
  123. </p>
  124. </div>
  125. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  126. <td align="left"></td>
  127. <td align="right"><div class="copyright-footer">Copyright &#169; 2010-2017 Tropic Software
  128. East Inc</div></td>
  129. </tr></table>
  130. <hr>
  131. <div class="spirit-nav">
  132. <a accesskey="p" href="../vmd_specific.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_identifier.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  133. </div>
  134. </body>
  135. </html>