Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Annex: No Variadic Macros

This section illustrates an alternative syntax for compilers without variadic macro support.

Sequence Syntax

Most modern compilers support variaid macros (notably, these include GCC, MSVC, and all C++11 compilers). However, in the rare case that programmers need to use this library on a compiler without variadic macros, this library also allows to specify its macro parameters using a Boost.Preprocessor sequence where tokens are separated by round parenthesis ():

(token1) (token2) ... // All compilers.

Instead of the comma-separated list that we have seen so far which requires variadic macros:

token1, token2, ... // Only compilers with varidic macros.

For example, the following syntax is accepted on all compilers with and without variadic macros (see also add_seq.cpp):

int main(void) {
    int sum = 0, factor = 10;

    void BOOST_LOCAL_FUNCTION( (const bind factor) (bind& sum) (int num) ) {
        sum += factor * num;
    } BOOST_LOCAL_FUNCTION_NAME(add)

    add(1);
    int nums[] = {2, 3};
    std::for_each(nums, nums + 2, add);

    BOOST_TEST(sum == 60);
    return boost::report_errors();
}

However, on compilers with variadic macros the comma-separated syntax we have seen so far is preferred because more readable (see also add.cpp):

int main(void) {                            // Some local scope.
    int sum = 0, factor = 10;               // Variables in scope to bind.

    void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
        sum += factor * num;
    } BOOST_LOCAL_FUNCTION_NAME(add)

    add(1);                                 // Call the local function.
    int nums[] = {2, 3};
    std::for_each(nums, nums + 2, add);     // Pass it to an algorithm.

    BOOST_TEST(sum == 60);                  // Assert final summation value.
    return boost::report_errors();
}

Note that the same macros accept both syntaxes on compilers with variadic macros and only the sequence syntax on compilers without variadic macros. Finally, an empty local function parameter list is always specified using void on compilers with and without variadic macros:

int BOOST_LOCAL_FUNCTION(void) { // No parameter.
    return 10;
} BOOST_LOCAL_FUNCTION_NAME(ten)

BOOST_TEST(ten() == 10);

Examples

For reference, the following is a list of most of the examples presented in this documentation reprogrammed using the sequence syntax instead of the comma-separated syntax (in alphabetic order):

Files

add_default_seq.cpp

add_except_seq.cpp

add_inline_seq.cpp

add_params_only_seq.cpp

add_template_seq.cpp

add_this_seq.cpp

add_typed_seq.cpp

add_with_default_seq.cpp

all_decl_seq.cpp

factorial_seq.cpp

macro_commas_seq.cpp

nesting_seq.cpp

overload_seq.cpp

return_assign_seq.cpp

return_derivative_seq.cpp

return_inc_seq.cpp

return_setget_seq.cpp

return_this_seq.cpp

same_line_seq.cpp

transform_seq.cpp

typeof_seq.cpp

typeof_template_seq.cpp


PrevUpHomeNext