|
BDE 4.39.x Production Release
|
Provide macros for use with NULL-terminated variadic functions.
Provide macros for use with NULL-terminated variadic functions.
NULLARG_IDX is not NULLBSLA_NULLTERMINATED is activeBSLA_NULLTERMINATEDAT activeThis component provides preprocessor macros to indicate that a variadic function's arguments are terminated by NULL, or, in the case of BSLA_NULLTERMINATEDAT, by NULL at a certain index. Note that the terminating NULL must actually be NULL or, with C++11, nullptr; passing 0 in its place will result in a warning.
BSLA_NULLTERMINATED() This annotation on a variadic macro indicates that a warning should be issued unless the last argument to the function is explicitly
NULL.
BSLA_NULLTERMINATEDAT(ARG_IDX): This annotation on a variadic function indicates that a warning should be issued unless the argument at
ARG_IDXisNULL, whereARG_IDXis the number of arguments from the last, the last argument havingARG_IDX == 0. Thus,BSLA_NULLTERMINATEDis equivalent toBSLA_NULLTERMINATEDAT(0).
BSLA_NULLTERMINATED_IS_ACTIVE: The macro
BSLA_NULLTERMINATED_IS_ACTIVEis defined ifBSLA_NULLTERMINATEDexpands to something with the desired effect; otherwiseBSLA_NULLTERMINATED_IS_ACTIVEis not defined andBSLA_NULLTERMINATEDexpands to nothing.
BSLA_NULLTERMINATEDAT_IS_ACTIVE: The macro
BSLA_NULLTERMINATEDAT_IS_ACTIVEis defined ifBSLA_NULLTERMINATEDATexpands to something with the desired effect; otherwiseBSLA_NULLTERMINATEDAT_IS_ACTIVEis not defined andBSLA_NULLTERMINATEDATexpands to nothing.
This section illustrates intended use of this component.
Suppose we want to have a function that, passed a variable length argument list of const char * strings terminated by NULL, concatenates the strings, separated by spaces, into a buffer.
First, we declare and define the function, annotated with BSLA_NULL_TERMINATED:
Then, in main, we call catStrings correctly:
which compiles without a warning and produces the output:
Now, we call catStrings again and forget to add the terminating ‘NULL’:
Finally, we get the compiler warning:
Suppose we want to have a function that, passed a variable length argument list of const char * strings terminated by NULL, concatenates the strings, separated by spaces, into a buffer, and then there's an additional integer argument, interpreted as a boolean, that determines what is to be appended to the end of the buffer.
First, we declare and define the function, annotated with BSLA_NULL_TERMINATEDAT(1):
Then, in main, we call catVerdict correctly:
which compiles without a warning and produces the output:
Next, we call catVerdict with no NULL passed, and get a warning (and probably a core dump if we ran it):
And we get the following compiler warning:
Now, we call catVerdict and forget to put the integer that indicates guilt or innocence after the NULL. This means that NULL is happening at index 0, not index 1, which violates the requirement imposed by the annotation:
Finally, we get the compiler warning:
| #define BSLA_NULLTERMINATED |
| #define BSLA_NULLTERMINATEDAT | ( | ARG_IDX | ) |