]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/vmd/doc/vmd_empty.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / vmd / doc / vmd_empty.qbk
CommitLineData
7c673cae
FG
1[/
2 (C) Copyright Edward Diener 2011-2015
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at
5 http://www.boost.org/LICENSE_1_0.txt).
6]
7
8[section:vmd_test_empty Emptiness]
9
10[heading Passing empty arguments]
11
12It is possible to pass an empty argument to a macro.
13The official terminology for this in the C++ standard is an argument
14"consisting of no preprocessing tokens".
15
16Let us consider a number of cases without worrying too much
17what the macro output represents.
18
19Consider these two function-like macros:
20
21 #define SMACRO() someoutput
22 #define EMACRO(x) otheroutput x
23
24The first macro takes no parameters so invoking it must always be done by
25
26 SMACRO()
27
28and passing any arguments to it would be invalid.
29
30The second macro takes a single parameter. it can be evoked as
31
32 EMACRO(somedata)
33
34but it also can be invoked as
35
36 EMACRO()
37
38In the second invocation of EMACRO we are passing an empty argument to the macro.
39Similarly for any macro having 1 or more parameters, an empty argument
40can be validly passed for any of the parameters, as in
41
42 #define MMACRO(x,y,z) x y z
43
44 MMACRO(1,,2)
45
46An empty argument is an argument even if we are passing nothing.
47
48Because an empty argument can be passed for a given parameter of
49a macro does not mean one should do so. Any given macro will specify what each
50argument to a macro should represent, and it is has normally been very rare to encounter
51a macro which specifies that an empty argument can logically be passed for a given
52argument. But from the perspective of standard C++ it is perfectly valid to
53pass an empty argument for a macro parameter.
54
55The notion of passing empty arguments can be extended to passing empty data which
56"consists of no preprocessing tokens" in slightly more complicated situations.
57It is possible to pass empty data as an argument to a variadic macro in the form of
58variadic macro data, as in
59
60 #define VMACRO(x,...) x __VA_ARGS__
61
62invoked as
63
64 VMACRO(somedata,)
65
66Here one passes empty data as the variadic macro data and it is perfectly valid C++.
67Please notice that this different from
68
69 VMACRO(somedata)
70
71which is not valid C++ since something must be passed for the variadic argument.
72Similar one could invoke the macro as
73
74 VMACRO(somedata,vdata1,,vdata3)
75
76where one is passing variadic macro data but an element in the variadic macro data is empty.
77
78Furthermore if we are invoking a macro which expects a Boost PP data type, such as
79a tuple, we could also validly pass empty data for all or part of the data in a tuple,
80as in
81
82 #define TMACRO(x,atuple) x atuple
83
84 TMACRO(somedata,())
85
86In this case we are passing a 1 element tuple
87where the single element itself is empty.
88
89or
90
91 TMACRO(somedata,(telem1,,telem2,teleem3))
92
93In this case we are passing a 4 element tuple where
94the second element is empty.
95
96Again either invocation is valid C++ but it is not necessarily what the
97designed of the macro has desired, even if in both cases the macro designer
98has specified that the second parameter must be a tuple for the macro
99to work properly.
100
101[heading Returning emptiness]
102
103Similar to passing empty arguments in various ways to a macro,
104the data which a macro returns ( or 'generates' may be a better term )
105could be empty, in various ways. Again I am not necessarily promoting
106this idea as a common occurrence of macro design but merely pointing it
107out as valid C++ preprocessing.
108
109 #define RMACRO(x,y,z)
110
111 RMACRO(data1,data2,data3)
112
113It is perfectly valid C++ to return "nothing" from a macro invocation.
114In fact a number of macros in Boost PP do that based on the preprocessor
115metaprogramming logic of the macro, and are documented as such.
116
117Similarly one could return nothing as part or all of a Boost PP
118data type or even as part of variadic macro data.
119
120 #define TRETMACRO(x,y,z) ()
121 #define TRETMACRO1(x,y,z) (x,,y,,z)
122 #define VRETMACRO(x,y,z) x,,y,,z
123
124Here again we are returning something but in terms of a Boost PP
125tuple or in terms of variadic data, we have elements which are empty.
126
127[heading Emptiness in preprocessor metaprogramming]
128
129In the examples given above where "emptiness" in one form of another
130is passed as arguments to a macro or returned from a macro, the examples
131I have given were created as simplified as possible to illustrate my points.
132In actual preprocessor metaprogramming, using Boost PP, where complicated logic
133is used to generate macro output based on the arguments to a macro, it might be
134useful to allow and work with empty data if one were able to test for the fact
135that data was indeed empty.
136
137[heading Testing for empty data]
138
139Currently Boost PP has an undocumented macro for testing whether
140a parameter is empty of not, written without the use of variadic
141macros. The macro is called BOOST_PP_IS_EMPTY. The macro is by its nature flawed,
142since there is no generalized way of determining whether or not a
143parameter is empty using the C++ preprocessor. But the macro will
144work given input limited in various ways or if the input is actually empty.
145
146Paul Mensonides, the developer of Boost PP and the BOOST_PP_IS_EMPTY macro
147in that library, also wrote a better macro using variadic
148macros, for determining whether or not a parameter is empty or not, which
149he published on the Internet in response to a discussion about emptiness.
150This macro is also not perfect, since there is no perfect solution,
151but will work correctly with almost all input. I have adapted his code
152for the VMD and developed my own very slightly different code.
153
154The macro is called [macroref BOOST_VMD_IS_EMPTY] and will return 1 if its input
155is empty or 0 if its input is not empty. The macro
156is a variadic macro which make take any input
157[footnote For VC++ 8 the input is not variadic data but a single parameter].
158
159[heading Macro Flaw with a standard C++ compiler]
160
161The one situation where the macro always does not work properly is if its input resolves
162to a function-like macro name or a sequence of preprocessor tokens ending with
163a function-like macro name and the function-like macro takes two or more parameters.
164
165Here is a simple example:
166
167 #include <boost/vmd/is_empty.hpp>
168
169 #define FMACRO(x,y) any_output
170
171 BOOST_VMD_IS_EMPTY(FMACRO)
172 BOOST_VMD_IS_EMPTY(some_input FMACRO)
173
174In the first case the name of a function-like macro is being passed to
175BOOST_VMD_IS_EMPTY while in the second case a sequence of preprocessing tokens is being
176passed to BOOST_VMD_IS_EMPTY ending with the name of a function-like macro.
177The function-like macro also has two ( or more ) parameters. In both the
178cases above a compiler error will result from the use of BOOST_VMD_IS_EMPTY.
179
180Please note that these two problematical cases are not the same as passing
181an invocation of a function-like macro name to BOOST_VMD_IS_EMPTY, as in
182
183 #include <boost/vmd/is_empty.hpp>
184
185 BOOST_VMD_IS_EMPTY(FMACRO(arg1,arg2))
186 BOOST_VMD_IS_EMPTY(someinput FMACRO(arg1,arg2))
187
188which always works correctly, unless of course a particular function-like macro
189invocation resolves to either of our two previous situations.
190
191Another situation where the macro may not work properly is if the previously mentioned
192function-like macro takes a single parameter but creates an error when the argument
193passed is empty. An example of this would be:
194
195 #define FMACRO(x) BOOST_PP_CAT(+,x C);
196
197When nothing is passed to FMACRO undefined behavior will occur since attempting to concatenate
198'+' to ' C' is UB in C++ preprocessor terms.
199
200So for a standard conforming compiler we have essentially a single corner case where
201the BOOST_VMD_IS_EMPTY does not work and, when it does not work it, produces a
202compiler error rather than an incorrect result. Essentially what is desired for maximum
203safety is that we never pass input ending with the name of a function-like macro name when
204testing for emptiness.
205
206[heading Macro Flaw with Visual C++]
207
208The VC++ preprocessor is not a standard C++ conforming preprocessor in at least two
209relevant situations to our discussion of emptiness. These situations combine to create
210a single corner case which causes the BOOST_VMD_IS_EMPTY macro to not work properly
211using VC++ when the input resolves to a function-like macro name.
212
213The first situation, related to our discussion of emptiness, where the VC++ preprocessor
214is not a standard C++ conforming preprocessor is that if a macro taking 'n' number of parameters is invoked
215with 0 to 'n-1' parameters, the compiler does not give an error, but only a warning.
216
217 #define FMACRO(x,y) x + y
218
219 FMACRO(1)
220
221should give a compiler error, as it does when using a C++ standard-conforming
222compiler, but when invoked using VC++ it only gives a warning
223and VC++ continues macro substitution with 'y' as a placemarker preprocessing token.
224This non-standard conforming action actually eliminates the case where BOOST_VMD_IS_EMPTY
225does not work properly with a standard C++ conforming compiler. But of course it has the
226potential of producing incorrect output in other macro processing situations unrelated
227to the BOOST_VMD_IS_EMPTY invocation, where a compiler error should occur.
228
229A second general situation, related to our discussion of emptiness, where the VC++ preprocessor
230is not a standard C++ conforming preprocessor is that the expansion of a macro works incorrectly
231when the expanded macro is a function-like macro name followed by a function-like macro invocation,
232in which case the macro re-expansion is erroneously done more than once. This latter case can be
233seen by this example:
234
235 #define FMACRO1(parameter) FMACRO3 parameter()
236 #define FMACRO2() ()
237 #define FMACRO3() 1
238
239 FMACRO1(FMACRO2)
240
241 should expand to:
242
243 FMACRO3()
244
245 but in VC++ it expands to:
246
247 1
248
249where after initially expanding the macro to:
250
251 FMACRO3 FMACRO2()
252
253VC++ erroneously rescans the sequence of preprocessing tokens more than once rather than
254rescan just one more time for more macro names.
255
256What these two particular preprocessor flaws in the VC++ compiler mean is that although
257BOOST_VMD_IS_EMPTY does not fail with a compiler error in the same case as with
258a standard C++ conforming compiler given previously, it fails by giving
259the wrong result in another situation.
260
261The failing situation is:
262
263when the input to BOOST_VMD_IS_EMPTY resolves to only a function-like macro
264name, and the function-like macro, when passed a single empty argument, expands to
265a Boost PP tuple, BOOST_VMD_IS_EMPTY will erroneously return 1 when using the Visual C++
266compiler rather than either give a preprocessing error or return 0.
267
268Here is an example of the failure:
269
270 #include <boost/vmd/is_empty.hpp>
271
272 #define FMACRO4() ( any_number_of_tuple_elements )
273 #define FMACRO5(param) ( any_number_of_tuple_elements )
274 #define FMACRO6(param1,param2) ( any_number_of_tuple_elements )
275
276 BOOST_VMD_IS_EMPTY(FMACRO4) // erroneously returns 1, instead of 0
277 BOOST_VMD_IS_EMPTY(FMACRO5) // erroneously returns 1, instead of 0
278 BOOST_VMD_IS_EMPTY(FMACRO6) // erroneously returns 1, instead of generating a preprocessing error
279
280As with a standard C++ conforming compiler, we have a rare corner case where
281the BOOST_VMD_IS_EMPTY will not work properly, but unfortunately in this very
282similar but even rarer corner case with VC++, we will silently get an incorrect result
283rather than a compiler error.
284
285I want to reiterate that there is no perfect solution in C++ to the detection of
286emptiness even for a C++ compiler whose preprocessor is completely conformant, which
287VC++ obviously is not.
288
289[heading Macro Flaw conclusion]
290
291With all of the above mentioned, the case(s) where BOOST_VMD_IS_EMPTY will work
292incorrectly are very small, even with the erroneous VC++ preprocessor,
293and I consider the macro worthwhile to use since it works correctly with the vast
294majority of possible preprocessor input.
295
296The case where it will not work, with both a C++ standard conforming preprocessor or
297with Visual C++, occurs when the name of a function-like macro is part of the input
298to BOOST_VMD_IS_EMPTY. Obviously the macro should be used by the preprocessor
299metaprogrammer when the possible input to it is constrained to eliminate the erroneous
300case.
301
302Furthermore, since emptiness can correctly be tested for in nearly every situation, the
303BOOST_VMD_IS_EMPTY macro can be used internally when the preprocessor metaprogrammer wants to return data
304from a macro and all or part of that data could be empty.
305
306Therefore I believe the BOOST_VMD_IS_EMPTY macro is quite useful, despite the corner case flaw
307which makes it imperfect. Consequently I believe that the preprocessor metaprogrammer
308can use the concept of empty preprocessor data in the design of his own macros.
309
310[heading Using the macro]
311
312The macro BOOST_VMD_IS_EMPTY is used internally throughout VMD and macro programmers
313may find this macro useful in their own programming efforts despite the slight flaw
314in the way that it works.
315
316You can use the general header file:
317
318 #include <boost/vmd/vmd.hpp>
319
320or you can use the individual header file:
321
322 #include <boost/vmd/is_empty.hpp>
323
324for the BOOST_VMD_IS_EMPTY macro.
325
326[endsect]