Added true_type and false_type to aid user-defined specialisations.

[SVN r21269]
This commit is contained in:
John Maddock
2003-12-15 12:01:53 +00:00
parent 8aaa85b7c3
commit b55dc5cc24
3 changed files with 80 additions and 2 deletions

View File

@ -16,7 +16,7 @@
<a href="#transformations">Transformations Between Types</a>
<a href="#synthesized">Synthesizing Types</a>
<a href="#function_traits">Function Traits</a>
<a href="#headers">Type traits headers</a>
<a href="#headers">Type traits headers</a><BR><A href="#specializations">User defined specializations</A>
<a href="#example">Example Code</a></pre>
<h2><a name="intro"></a>Introduction</h2>
<p>The contents of &lt;boost/type_traits.hpp&gt; are declared in namespace boost.</p>
@ -850,7 +850,40 @@ bool const y = boost::is_convertible&lt;D*,A*&gt;::value; // error
boost/type_traits/.&nbsp; So if for example some code requires
is_class&lt;&gt;, then just include:</p>
<pre>&lt;boost/type_traits/is_class.hpp&gt;</pre>
<h2><a name="example"></a>Example code</h2>
<h2><A name="specializations"></A>User defined specializations</h2>
<P>Occationally the end user may need to provide their own specialization for one
of the type traits - typically where intrinsic compiler support is required to
implement a specific trait fully.&nbsp; These specializations should derive
from boost::true_type or boost::false_type as appropriate:</P>
<PRE># include &lt;boost/type_traits/is_pod.hpp&gt;
# include &lt;boost/type_traits/is_class.hpp&gt;
# include &lt;boost/type_traits/is_union.hpp&gt;
struct my_pod{};
struct my_union
{
char c;
int i;
};
namespace boost
{
template&lt;&gt;
struct is_pod&lt;my_pod&gt;
: public true_type{};
template&lt;&gt;
struct is_pod&lt;my_union&gt;
: public true_type{};
template&lt;&gt;
struct is_union&lt;my_union&gt;
: public true_type{};
template&lt;&gt;
struct is_class&lt;my_union&gt;
: public false_type{};
}
</PRE>
<H2>Example code</H2>
<p>Type-traits comes with four example programs that illustrate some of the ways
in which the type traits templates may be used:</p>
<h4>Copy_example.cpp</h4>

View File

@ -84,8 +84,10 @@ test-suite type_traits :
[ type-traits-run tricky_is_enum_test.cpp ]
[ type-traits-run tricky_partial_spec_test.cpp ]
[ type-traits-run type_with_alignment_test.cpp ]
[ type-traits-run udt_specialisations.cpp ]
; # type traits suite

View File

@ -0,0 +1,43 @@
#include "test.hpp"
#include "check_integral_constant.hpp"
#ifdef TEST_STD
# include <type_traits>
#else
# include <boost/type_traits/is_pod.hpp>
# include <boost/type_traits/is_class.hpp>
# include <boost/type_traits/is_union.hpp>
#endif
struct my_pod{};
struct my_union
{
char c;
int i;
};
namespace tt
{
template<>
struct is_pod<my_pod>
: public true_type{};
template<>
struct is_pod<my_union>
: public true_type{};
template<>
struct is_union<my_union>
: public true_type{};
template<>
struct is_class<my_union>
: public false_type{};
}
TT_TEST_BEGIN(is_pod)
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_pod<my_pod>::value, true);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_pod<my_union>::value, true);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_union<my_union>::value, true);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_class<my_union>::value, false);
TT_TEST_END