...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
This section illustrates an alternative syntax for compilers without variadic macro support.
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);
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 |
---|