From cbc8f976c7c5dd2474d63ff052ac898d10748231 Mon Sep 17 00:00:00 2001 From: John Maddock Date: Mon, 31 Jul 2000 11:31:26 +0000 Subject: [PATCH] preview of updated version with is_convertible and alignment_of added. [SVN r7667] --- type_traits.htm | 78 +++++++++++++++++++++++--------- type_traits_test.cpp | 9 +++- type_traits_test.hpp | 105 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 23 deletions(-) create mode 100644 type_traits_test.hpp diff --git a/type_traits.htm b/type_traits.htm index dae21d7..61f2f59 100644 --- a/type_traits.htm +++ b/type_traits.htm @@ -25,6 +25,7 @@ divided up into the following sections:

Fundamental type operations
 Fundamental type properties
+   Miscellaneous
    cv-Qualifiers
    Fundamental Types
    Compound Types
@@ -37,7 +38,7 @@ divided up into the following sections:

Usage: "class_name<T>::type" performs indicated transformation on type T.

- +
@@ -108,39 +109,72 @@ indicated transformation on type T.

indicated property is true, false otherwise. (Note that class_name<T>::value is always defined as a compile time constant).

+

Miscellaneous

+ +

Expression.

+ + + + + + + + + + + + + + + + + + + + +

Expression

+

Description

+

Compiler

+
is_same<T,U>::value
+

True if T and U are the + same type.

+

P

+
is_convertible<T,U>::value
+

True if type T is + convertible to type U.

+
 
alignment_of<T>::value
+

An integral value + representing the minimum alignment requirements of type T.

+
 
+ +

 

+

cv-Qualifiers

The following classes determine what cv-qualifiers are present on a type (see 3.93).

- +
- - - - - + - - - + - - - - - -

Expression.

+

Expression.

Description.

+

Description.

Compiler.

+

Compiler.

is_const<T>::valueTrue if type T is top-level + is_const<T>::valueTrue if type T is top-level const qualified.

P

+

P

is_volatile<T>::valueTrue if type T is top-level + is_volatile<T>::valueTrue if type T is top-level volatile qualified.

P

-
is_same<T,U>::valueTrue if T and U are the same - type.

P

+

P

@@ -152,7 +186,7 @@ on a type (see 3.93).

The following will only ever be true for cv-unqualified types; these are closely based on the section 3.9 of the C++ Standard.

- +
@@ -291,7 +325,7 @@ these are closely based on the section 3.9 of the C++ Standard.

The following will only ever be true for cv-unqualified types, as defined by the Standard. 

-

Expression.

+
@@ -365,7 +399,7 @@ as defined by the Standard. 

is true then class_name<cv-qualified-T>::value will also be true.

-

Expression

+
@@ -582,7 +616,7 @@ Hinnant and John Maddock.

Maintained by John Maddock, the latest version of this file can be found at www.boost.org, and the boost +href="http://www.boost.org/">www.boost.org, and the boost discussion list at www.egroups.com/list/boost.

diff --git a/type_traits_test.cpp b/type_traits_test.cpp index b763bfe..86a3787 100644 --- a/type_traits_test.cpp +++ b/type_traits_test.cpp @@ -5,6 +5,8 @@ // warranty, and with no claim as to its suitability for any purpose. /* Release notes: + 31st July 2000: + Added extra tests for is_empty, is_convertible, alignment_of. 23rd July 2000: Removed all call_traits tests to call_traits_test.cpp Removed all compressed_pair tests to compressed_pair_tests.cpp @@ -570,7 +572,12 @@ int main() align_test(char); align_test(double); align_test(int[4]); - //align_test(int&); + align_test(int(*)(int)); +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + align_test(char&); + align_test(char (&)(int)); + align_test(char(&)[4]); +#endif align_test(int*); //align_test(const int); align_test(VB); diff --git a/type_traits_test.hpp b/type_traits_test.hpp new file mode 100644 index 0000000..20b32d4 --- /dev/null +++ b/type_traits_test.hpp @@ -0,0 +1,105 @@ + // boost::compressed_pair test program + + // (C) Copyright John Maddock 2000. Permission to copy, use, modify, sell and + // distribute this software is granted provided this copyright notice appears + // in all copies. This software is provided "as is" without express or implied + // warranty, and with no claim as to its suitability for any purpose. + + + +#ifndef BOOST_TYPE_TRAITS_TEST_HPP +#define BOOST_TYPE_TRAITS_TEST_HPP + +// +// this one is here just to suppress warnings: +// +template +bool do_compare(T i, T j) +{ + return i == j; +} + +// +// this one is to verify that a constant is indeed a +// constant-integral-expression: +// +template +struct ct_checker +{ +}; + +#define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y) +#define BOOST_DO_JOIN2(X, Y) X ## Y +#define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y ) + + +#define value_test(v, x) ++test_count;\ + typedef ct_checker<(x)> BOOST_JOIN(this_is_a_compile_time_check_, __LINE__);\ + if(!do_compare((int)v,(int)x)){++failures; std::cout << "checking value of " << #x << "...failed" << std::endl;} +#define value_fail(v, x) ++test_count; ++failures; std::cout << "checking value of " << #x << "...failed" << std::endl; + +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION +#define type_test(v, x) ++test_count;\ + if(do_compare(boost::is_same::value, false)){\ + ++failures; \ + std::cout << "checking type of " << #x << "...failed" << std::endl; \ + std::cout << " expected type was " << #v << std::endl; \ + std::cout << " " << typeid(boost::is_same).name() << "::value is false" << std::endl; } +#else +#define type_test(v, x) ++test_count;\ + if(typeid(v) != typeid(x)){\ + ++failures; \ + std::cout << "checking type of " << #x << "...failed" << std::endl; \ + std::cout << " expected type was " << #v << std::endl; \ + std::cout << " " << "typeid(" #v ") != typeid(" #x ")" << std::endl; } +#endif + +template +struct test_align +{ + struct padded + { + char c; + T t; + }; + static void do_it() + { + padded p; + unsigned a = reinterpret_cast(&(p.t)) - reinterpret_cast(&p); + value_test(a, boost::alignment_of::value); + } +}; +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION +template +struct test_align +{ + static void do_it() + { + // + // we can't do the usual test because we can't take the address + // of a reference, so check that the result is the same as for a + // pointer type instead: + value_test(boost::alignment_of::value, boost::alignment_of::value); + } +}; +#endif + +#define align_test(T) test_align::do_it() + +// +// define tests here +unsigned failures = 0; +unsigned test_count = 0; + +// +// turn off some warnings: +#ifdef __BORLANDC__ +#pragma option -w-8004 +#endif + +#ifdef BOOST_MSVC +#pragma warning (disable: 4018) +#endif + + +#endif // BOOST_TYPE_TRAITS_TEST_HPP

Expression