###

# Boost.Preprocessor

## Tutorial examples preprocessed

The following code snippets were produced by actually preprocessing the code snippets of the tutorial. After preprocessing the code was reformatted manually.

EXAMPLE: Use a Local Macro to avoid small scale repetition

>     template<class T, int n>
>     vec<T,n>&
>       operator +=
>       ( vec<T,n>&
>           lhs
>       , const vec<T,n>&
>           rhs
>       )
>     { for (int i=0; i<n; ++i)
>         lhs(i) += rhs(i);
>       return lhs;
>     }

>     template<class T, int n>
>     vec<T,n>&
>       operator -=
>       ( vec<T,n>&
>           lhs
>       , const vec<T,n>&
>           rhs
>       )
>     { for (int i=0; i<n; ++i)
>         lhs(i) -= rhs(i);
>       return lhs;
>     }

>     template<class T, int n>
>     vec<T,n>&
>       operator *=
>       ( vec<T,n>&
>           lhs
>       , const vec<T,n>&
>           rhs
>       )
>     { for (int i=0; i<n; ++i)
>         lhs(i) *= rhs(i);
>       return lhs;
>     }

>     template<class T, int n>
>     vec<T,n>&
>       operator /=
>       ( vec<T,n>&
>           lhs
>       , const vec<T,n>&
>           rhs
>       )
>     { for (int i=0; i<n; ++i)
>         lhs(i) /= rhs(i);
>       return lhs;
>     }

EXAMPLE: Use BOOST_PP_EMPTY() as an unused parameter in Local Macro instantiations

>     template<class base>
>     typename implement_subscript_using_begin_subscript<base>::value_type&
>       implement_subscript_using_begin_subscript<base>::operator[]
>       ( index_type
>           i
>       )
>     { return base::begin()[i];
>     }

>     template<class base>
>     const typename implement_subscript_using_begin_subscript<base>::value_type&
>       implement_subscript_using_begin_subscript<base>::operator[]
>       ( index_type
>           i
>       ) const
>     { return base::begin()[i];
>     }

EXAMPLE: Use BOOST_PP_CAT instead of ## when necessary

>     enum
>     { static_check_152 = (sizeof(int) <= sizeof(long)) ? 1 : -1
>     };
>     typedef char
>       static_assert_152
>       [ static_check_152
>       ];

EXAMPLE: Use BOOST_PP_STRINGIZE instead of # whenever necessary

>     #pragma message("examples.cpp" "(" "20" ") : " "TBD!")

EXAMPLE: Use:

- BOOST_PP_ENUM_PARAMS,
- BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT,
- BOOST_PP_ENUM_PARAMS_WITH_DEFAULTS,
- BOOST_PP_ENUM_SHIFTED_PARAMS, or
- BOOST_PP_REPEAT, and
- BOOST_PP_COMMA_IF

to avoid O(N) repetition on lists in general

>     struct make_type_list_end;

>     template
>     < class T0=make_type_list_end
>     , class T1=make_type_list_end
>     , class T2=make_type_list_end
>     , class T3=make_type_list_end
>     , class T4=make_type_list_end
>     , class T5=make_type_list_end
>     , class T6=make_type_list_end
>     , class T7=make_type_list_end
>     >
>     struct make_type_list
>     {
>     private:
>       enum
>       { end = is_same<T0,make_type_list_end>::value
>       };
>     public:
>       typedef typename
>         type_if
>         < end
>         , type_cons_empty
>         , type_cons
>           < T0
>           , typename
>             type_inner_if
>             < end
>             , type_identity<end>
>             , make_type_list
>               < T1
>               , T2
>               , T3
>               , T4
>               , T5
>               , T6
>               , T7
>               >
>             >::type
>           >
>         >::type type;
>     };

EXAMPLE: Use BOOST_PP_REPEAT and a Token Look-Up Function to eliminate categorical repetition

>     catch (bool t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (char t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (signed char t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (unsigned char t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (short t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (unsigned short t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (int t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (unsigned int t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (long t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (unsigned long t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (float t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (double t)
>     { report_typeid(t);
>       report_value(t);
>     }
>     catch (long double t)
>     { report_typeid(t);
>       report_value(t);
>     }

EXAMPLE: Use BOOST_PP_REPEAT_2ND to avoid O(N*N) repetition

>     vec()
>     {
>     }
>     vec(T a0)
>     { (*this)[0] = a0;
>     }
>     vec(T a0, T a1)
>     { (*this)[0] = a0;
>       (*this)[1] = a1;
>     }
>     vec(T a0, T a1, T a2)
>     { (*this)[0] = a0;
>       (*this)[1] = a1;
>       (*this)[2] = a2;
>     }
>     vec(T a0, T a1, T a2, T a3)
>     { (*this)[0] = a0;
>       (*this)[1] = a1;
>       (*this)[2] = a2;
>       (*this)[3] = a3;
>     }
>     vec(T a0, T a1, T a2, T a3, T a4)
>     { (*this)[0] = a0;
>       (*this)[1] = a1;
>       (*this)[2] = a2;
>       (*this)[3] = a3;
>       (*this)[4] = a4;
>     }
>     vec(T a0, T a1, T a2, T a3, T a4, T a5)
>     { (*this)[0] = a0;
>       (*this)[1] = a1;
>       (*this)[2] = a2;
>       (*this)[3] = a3;
>       (*this)[4] = a4;
>       (*this)[5] = a5;
>     }
>     vec(T a0, T a1, T a2, T a3, T a4, T a5, T a6)
>     { (*this)[0] = a0;
>       (*this)[1] = a1;
>       (*this)[2] = a2;
>       (*this)[3] = a3;
>       (*this)[4] = a4;
>       (*this)[5] = a5;
>       (*this)[6] = a6;
>     }
>     vec(T a0, T a1, T a2, T a3, T a4, T a5, T a6, T a7)
>     { (*this)[0] = a0;
>       (*this)[1] = a1;
>       (*this)[2] = a2;
>       (*this)[3] = a3;
>       (*this)[4] = a4;
>       (*this)[5] = a5;
>       (*this)[6] = a6;
>       (*this)[7] = a7;
>     }

EXAMPLE: Use BOOST_PP_IF to implement special case for the first element

>     false == false;
>     true == true;

EXAMPLE: Use arithmetic, logical and comparison operations when necessary

>     S, E0, E1
>     E0, S, E1
>     E0, E1, S
>     BAD PARAMS FOR SPECIAL_NUMBERED_LIST! E0, E1, E2, S

Revised

© Copyright Housemarque Oy 2002

Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.