mirror of
https://github.com/boostorg/preprocessor.git
synced 2025-07-03 07:46:31 +02:00
335 lines
7.0 KiB
HTML
335 lines
7.0 KiB
HTML
<html>
|
||
<head>
|
||
<title>Boost PREPROCESSOR library</title>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||
</head>
|
||
|
||
<body bgcolor="#FFFFFF">
|
||
<a href="index.htm"><IMG height=86
|
||
alt="c++boost.gif (8819 bytes)"
|
||
src="../../../c++boost.gif"
|
||
width=277 align=center></a>
|
||
<hr>
|
||
<h1>Boost PREPROCESSOR library: Tutorial examples preprocessed</h1>
|
||
|
||
<p>The following code snippets were produced by actually preprocessing the code
|
||
snippets of the tutorial. After preprocessing the code was reformatted manually.</p>
|
||
|
||
<hr>
|
||
<p><strong><a name="Local Macro"></a><a href="tutorial.htm#Local Macro">EXAMPLE</a>:</strong>
|
||
Use a Local Macro to avoid small scale repetition</p>
|
||
|
||
<blockquote>
|
||
<pre>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;
|
||
}
|
||
</pre>
|
||
</blockquote>
|
||
|
||
<hr>
|
||
<p><strong><a name="UNUSED"></a><a href="tutorial.htm#UNUSED">EXAMPLE</a>:</strong>
|
||
Use BOOST_PP_EMPTY() as an unused parameter in Local Macro instantiations</p>
|
||
|
||
<blockquote>
|
||
<pre>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];
|
||
}
|
||
</pre>
|
||
</blockquote>
|
||
|
||
<hr>
|
||
<p><b><a name="CAT"></a><a href="tutorial.htm#CAT">EXAMPLE:</a></b> Use BOOST_PP_CAT instead of ## when necessary</p>
|
||
|
||
<blockquote>
|
||
<pre>enum
|
||
{ static_check_152 = (sizeof(int) <= sizeof(long)) ? 1 : -1
|
||
};
|
||
typedef char
|
||
static_assert_152
|
||
[ static_check_152
|
||
];
|
||
</pre>
|
||
</blockquote>
|
||
<hr>
|
||
<p><b><a name="STRINGIZE"></a><a href="tutorial.htm#STRINGIZE">EXAMPLE:</a></b> Use BOOST_PP_STRINGIZE instead of # whenever necessary</p>
|
||
<blockquote>
|
||
<pre>#pragma message("examples.cpp" "(" "20" ") : " "TBD!")</pre>
|
||
</blockquote>
|
||
<hr>
|
||
<p><strong><a name="ENUM_PARAMS"></a><a href="tutorial.htm#ENUM_PARAMS">EXAMPLE</a>:</strong>
|
||
Use:</p>
|
||
<ul>
|
||
<li> BOOST_PP_ENUM_PARAMS,</li>
|
||
<li> BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT,</li>
|
||
<li> BOOST_PP_ENUM_PARAMS_WITH_DEFAULTS,</li>
|
||
<li> BOOST_PP_ENUM_SHIFTED_PARAMS, or</li>
|
||
<li>BOOST_PP_REPEAT, and</li>
|
||
<li> BOOST_PP_COMMA_IF</li>
|
||
</ul>
|
||
<p>to avoid O(N) repetition on lists in general</p>
|
||
<blockquote>
|
||
<pre>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;
|
||
};
|
||
</pre>
|
||
</blockquote>
|
||
|
||
<hr>
|
||
<p><strong><a name="Token Look-Up"></a><a href="tutorial.htm#Token Look-Up">EXAMPLE</a>:</strong>
|
||
Use BOOST_PP_REPEAT and a Token Look-Up Function to eliminate categorical
|
||
repetition</p>
|
||
|
||
<blockquote>
|
||
<pre>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);
|
||
}
|
||
</pre>
|
||
</blockquote>
|
||
|
||
<hr>
|
||
<p><strong><a name="2ND_REPEAT"></a><a href="tutorial.htm#2ND_REPEAT">EXAMPLE</a>:</strong>
|
||
Use BOOST_PP_REPEAT_2ND to avoid O(N*N) repetition</p>
|
||
|
||
<blockquote>
|
||
<pre>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;
|
||
}
|
||
</pre>
|
||
</blockquote>
|
||
|
||
<p>
|
||
<hr>
|
||
<p><a name="IF"></a><a href="tutorial.htm#IF"><b>EXAMPLE:</b></a>
|
||
Use BOOST_PP_IF to implement special case for the first element</p>
|
||
|
||
<blockquote>
|
||
<pre>false == false;
|
||
true == true;
|
||
</pre>
|
||
</blockquote>
|
||
|
||
<p>
|
||
<hr>
|
||
|
||
<p><a name="Arithmetic"></a><a href="tutorial.htm#Arithmetic"><B>EXAMPLE:</B></a> Use arithmetic, logical and comparison operations when necessary</p>
|
||
|
||
<blockquote>
|
||
<pre>S, E0, E1
|
||
E0, S, E1
|
||
E0, E1, S
|
||
BAD PARAMS FOR SPECIAL_NUMBERED_LIST! E0, E1, E2, S</pre>
|
||
</blockquote>
|
||
|
||
<hr>
|
||
<p><EFBFBD> Copyright Housemarque Oy 2001</p>
|
||
<p>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. </p>
|
||
<p>Updated: <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %b %Y" startspan --><!--webbot bot="Timestamp" endspan i-checksum="15246" -->
|
||
<p></p>
|
||
</body>
|
||
</html>
|