From b55dc5cc24fefba9a72de2fc3ef5b3a2a5ab6bd3 Mon Sep 17 00:00:00 2001 From: John Maddock Date: Mon, 15 Dec 2003 12:01:53 +0000 Subject: [PATCH] Added true_type and false_type to aid user-defined specialisations. [SVN r21269] --- index.html | 37 +++++++++++++++++++++++++++++-- test/Jamfile | 2 ++ test/udt_specialisations.cpp | 43 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 test/udt_specialisations.cpp diff --git a/index.html b/index.html index 72286c4..4cf5ffe 100644 --- a/index.html +++ b/index.html @@ -16,7 +16,7 @@ Transformations Between Types Synthesizing Types Function Traits -Type traits headers +Type traits headers
User defined specializations Example Code

Introduction

The contents of <boost/type_traits.hpp> are declared in namespace boost.

@@ -850,7 +850,40 @@ bool const y = boost::is_convertible<D*,A*>::value; // error boost/type_traits/.  So if for example some code requires is_class<>, then just include:

<boost/type_traits/is_class.hpp>
-

Example code

+

User defined specializations

+

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.  These specializations should derive + from boost::true_type or boost::false_type as appropriate:

+
#  include <boost/type_traits/is_pod.hpp>
+#  include <boost/type_traits/is_class.hpp>
+#  include <boost/type_traits/is_union.hpp>
+
+struct my_pod{};
+struct my_union
+{
+   char c;
+   int i;
+};
+
+namespace boost
+{
+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{};
+}
+
+      
+

Example code

Type-traits comes with four example programs that illustrate some of the ways in which the type traits templates may be used:

Copy_example.cpp

diff --git a/test/Jamfile b/test/Jamfile index f6999d0..5658d8f 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -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 + diff --git a/test/udt_specialisations.cpp b/test/udt_specialisations.cpp new file mode 100644 index 0000000..7554f3d --- /dev/null +++ b/test/udt_specialisations.cpp @@ -0,0 +1,43 @@ + +#include "test.hpp" +#include "check_integral_constant.hpp" +#ifdef TEST_STD +# include +#else +# include +# include +# include +#endif + +struct my_pod{}; +struct my_union +{ + char c; + int i; +}; + +namespace tt +{ +template<> +struct is_pod + : public true_type{}; +template<> +struct is_pod + : public true_type{}; +template<> +struct is_union + : public true_type{}; +template<> +struct is_class + : public false_type{}; +} + +TT_TEST_BEGIN(is_pod) + +BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_pod::value, true); +BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_pod::value, true); +BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_union::value, true); +BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_class::value, false); + +TT_TEST_END +