Implement conjunction, disjunction, negation

This commit is contained in:
Glen Fernandes
2020-05-31 02:47:41 -04:00
parent bd5bede326
commit f8e9879d4e
11 changed files with 315 additions and 0 deletions

36
doc/conjunction.qbk Normal file
View File

@ -0,0 +1,36 @@
[/
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt).
]
[section:conjunction conjunction]
template<class... T>
struct conjunction;
__inherit Inherits from the first type `U` in the list for which
`bool(U::value)` is `false`, or the last type in the list if there is no such
type. If `sizeof...(T)` is `0` then inherits from `__true_type`.
__header `#include <boost/type_traits/conjunction.hpp>`
[all_compilers] In the absence of variadic-template support, `conjunction` has
only 2 parameters.
__examples
[:Given: `template<int N> struct Int { static const int value = N };` ]
[:`conjunction<>` inherits from `__true_type`.]
[:`conjunction<Int<1> >` inherits from `Int<1>`.]
[:`conjunction<Int<1>, Int<2>, Int<3> >` inherits from `Int<3>`.]
[:`conjunction<Int<1>, Int<0>, Int<3> >` inherits from `Int<0>`.]
[endsect]

36
doc/disjunction.qbk Normal file
View File

@ -0,0 +1,36 @@
[/
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt).
]
[section:disjunction disjunction]
template<class... T>
struct disjunction;
__inherit Inherits from the first type `U` in the list for which
`bool(U::value)` is `true`, or the last type in the list if there is no such
type. If `sizeof...(T)` is `0` then inherits from `__false_type`.
__header `#include <boost/type_traits/disjunction.hpp>`
[all_compilers] In the absence of variadic-template support, `disjunction` has
only 2 parameters.
__examples
[:Given: `template<int N> struct Int { static const int value = N };` ]
[:`disjunction<>` inherits from `__false_type`.]
[:`disjunction<Int<1> >` inherits from `Int<1>`.]
[:`disjunction<Int<1>, Int<2>, Int<3> >` inherits from `Int<1>`.]
[:`disjunction<Int<0>, Int<2>, Int<3> >` inherits from `Int<2>`.]
[endsect]

35
doc/negation.qbk Normal file
View File

@ -0,0 +1,35 @@
[/
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt).
]
[section:negation negation]
template<class T>
struct negation
: public __integral_constant<bool, !bool(T::value)> { };
__inherit Inherits from `__integral_constant<bool, !bool(T::value)>`.
__header `#include <boost/type_traits/negation.hpp>`
[all_compilers]
__examples
[:`negation<__true_type>` inherits from `__false_type`.]
[:`negation<__false_type>` inherits from `__true_type`.]
[:`negation<__integral_constant<int, 4> >::type` is the type `__false_type`.]
[:`negation<__integral_constant<int, 0> >::value` is an integral constant
expression that evaluates to /true/.]
[:`negation<T>::value_type` is the type `bool`.]
[endsect]

View File

@ -129,6 +129,9 @@
[def __add_cv [link boost_typetraits.reference.add_cv add_cv]]
[def __common_type [link boost_typetraits.reference.common_type common_type]]
[def __conditional [link boost_typetraits.reference.conditional conditional]]
[def __conjunction [link boost_typetraits.reference.conjunction conjunction]]
[def __disjunction [link boost_typetraits.reference.disjunction disjunction]]
[def __negation [link boost_typetraits.reference.negation negation]]
[def __type_with_alignment [link boost_typetraits.reference.type_with_alignment type_with_alignment]]
[def __aligned_storage [link boost_typetraits.reference.aligned_storage aligned_storage]]
@ -307,6 +310,7 @@ that is the result of the transformation.
[include aligned_storage.qbk]
[include alignment_of.qbk]
[include conditional.qbk]
[include conjunction.qbk]
[include common_type.qbk]
[include copy_cv.qbk]
[include copy_cv_ref.qbk]
@ -315,6 +319,7 @@ that is the result of the transformation.
[include declval.qbk]
[include detected.qbk]
[include detected_or.qbk]
[include disjunction.qbk]
[include enable_if.qbk]
[include extent.qbk]
[include floating_point_promotion.qbk]
@ -441,6 +446,7 @@ See __has_trivial_constructor.
[include make_signed.qbk]
[include make_unsigned.qbk]
[include make_void.qbk]
[include negation.qbk]
[include nonesuch.qbk]
[include promote.qbk]

View File

@ -21,11 +21,13 @@
#include <boost/type_traits/alignment_of.hpp>
#include <boost/type_traits/common_type.hpp>
#include <boost/type_traits/conditional.hpp>
#include <boost/type_traits/conjunction.hpp>
#include <boost/type_traits/copy_cv.hpp>
#include <boost/type_traits/copy_cv_ref.hpp>
#include <boost/type_traits/copy_reference.hpp>
#include <boost/type_traits/decay.hpp>
#include <boost/type_traits/declval.hpp>
#include <boost/type_traits/disjunction.hpp>
#include <boost/type_traits/enable_if.hpp>
#include <boost/type_traits/extent.hpp>
#include <boost/type_traits/floating_point_promotion.hpp>
@ -136,6 +138,7 @@
#include <boost/type_traits/make_signed.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/type_traits/make_void.hpp>
#include <boost/type_traits/negation.hpp>
#include <boost/type_traits/rank.hpp>
#include <boost/type_traits/remove_all_extents.hpp>
#include <boost/type_traits/remove_bounds.hpp>

View File

@ -0,0 +1,40 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_TT_CONJUNCTION_HPP_INCLUDED
#define BOOST_TT_CONJUNCTION_HPP_INCLUDED
#include <boost/type_traits/conditional.hpp>
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
#include <boost/type_traits/integral_constant.hpp>
#endif
namespace boost {
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class...>
struct conjunction
: true_type { };
template<class T>
struct conjunction<T>
: T { };
template<class T, class... U>
struct conjunction<T, U...>
: conditional<bool(T::value), conjunction<U...>, T>::type { };
#else
template<class T, class U>
struct conjunction
: conditional<bool(T::value), U, T>::type { };
#endif
} /* boost */
#endif

View File

@ -0,0 +1,40 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_TT_DISJUNCTION_HPP_INCLUDED
#define BOOST_TT_DISJUNCTION_HPP_INCLUDED
#include <boost/type_traits/conditional.hpp>
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
#include <boost/type_traits/integral_constant.hpp>
#endif
namespace boost {
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class...>
struct disjunction
: false_type { };
template<class T>
struct disjunction<T>
: T { };
template<class T, class... U>
struct disjunction<T, U...>
: conditional<bool(T::value), T, disjunction<U...> >::type { };
#else
template<class T, class U>
struct disjunction
: conditional<bool(T::value), T, U>::type { };
#endif
} /* boost */
#endif

View File

@ -0,0 +1,23 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_TT_NEGATION_HPP_INCLUDED
#define BOOST_TT_NEGATION_HPP_INCLUDED
#include <boost/type_traits/integral_constant.hpp>
namespace boost {
template<class T>
struct negation
: integral_constant<bool, !bool(T::value)> { };
} /* boost */
#endif

35
test/conjunction_test.cpp Normal file
View File

@ -0,0 +1,35 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifdef TEST_STD
#include <type_traits>
#else
#include <boost/type_traits/conjunction.hpp>
#endif
#include "check_integral_constant.hpp"
template<int V>
struct Int {
static const int value = V;
};
TT_TEST_BEGIN(conjunction)
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::conjunction<Int<2>, Int<4> >::value), 4);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::conjunction<Int<0>, Int<4> >::value), 0);
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::conjunction<Int<2>, Int<4>, Int<6>,
Int<8>, Int<10> >::value), 10);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::conjunction<Int<2>, Int<0>, Int<4>,
Int<6>, Int<8> >::value), 0);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::conjunction<Int<4> >::value, 4);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::conjunction<>::value, true);
#endif
TT_TEST_END

35
test/disjunction_test.cpp Normal file
View File

@ -0,0 +1,35 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifdef TEST_STD
#include <type_traits>
#else
#include <boost/type_traits/disjunction.hpp>
#endif
#include "check_integral_constant.hpp"
template<int V>
struct Int {
static const int value = V;
};
TT_TEST_BEGIN(disjunction)
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::disjunction<Int<2>, Int<4> >::value), 2);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::disjunction<Int<0>, Int<4> >::value), 4);
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::disjunction<Int<2>, Int<4>, Int<6>,
Int<8>, Int<10> >::value), 2);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::disjunction<Int<0>, Int<0>, Int<6>,
Int<0>, Int<0> >::value), 6);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::disjunction<Int<4> >::value, 4);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::disjunction<>::value, false);
#endif
TT_TEST_END

26
test/negation_test.cpp Normal file
View File

@ -0,0 +1,26 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifdef TEST_STD
#include <type_traits>
#else
#include <boost/type_traits/negation.hpp>
#endif
#include "check_integral_constant.hpp"
template<int V>
struct Int {
static const int value = V;
};
TT_TEST_BEGIN(negation)
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::negation<Int<5> >::value, false);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::negation<Int<0> >::value, true);
TT_TEST_END