1
0
forked from boostorg/mp11

Merge branch 'feature/mp_similar' into develop

This commit is contained in:
Peter Dimov
2019-01-07 17:02:03 +02:00
4 changed files with 112 additions and 4 deletions

View File

@@ -100,6 +100,28 @@ using R4 = mp_any<void, mp_true>; // compile-time error
`mp_same<T...>` is `mp_true` if all the types in `T...` are the same type, `mp_false` otherwise. `mp_same<>` is `mp_true`.
## mp_similar<T...>
template<class... T> using mp_similar = /*...*/;
`mp_similar<T...>` is `mp_true` if all the types in `T...` are the same type, or instantiations of the same class template
whose parameters are all types, `mp_false` otherwise. `mp_similar<>` is `mp_true`.
.mp_similar
```
using R1 = mp_similar<void>; // mp_true
using R2 = mp_similar<void, void>; // mp_true
using R3 = mp_similar<void, void, void>; // mp_true
using R4 = mp_similar<void, void, float>; // mp_false
template<class T> struct X;
template<class... T> struct Y;
using R5 = mp_similar<X<int>, X<void>, X<float>>; // mp_true
using R6 = mp_similar<Y<>, Y<void>, Y<void, void>>; // mp_true
using R7 = mp_similar<X<void>, Y<void>>; // mp_false
```
## mp_plus<T...>
template<class... T> using mp_plus = /*...*/;

View File

@@ -1,12 +1,12 @@
#ifndef BOOST_MP11_FUNCTION_HPP_INCLUDED
#define BOOST_MP11_FUNCTION_HPP_INCLUDED
// Copyright 2015-2017 Peter Dimov.
// Copyright 2015-2019 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
// 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
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/integral.hpp>
#include <boost/mp11/utility.hpp>
@@ -155,6 +155,46 @@ template<class T1, class... T> struct mp_same_impl<T1, T...>
template<class... T> using mp_same = typename detail::mp_same_impl<T...>::type;
// mp_similar<T...>
namespace detail
{
template<class... T> struct mp_similar_impl;
template<> struct mp_similar_impl<>
{
using type = mp_true;
};
template<class T> struct mp_similar_impl<T>
{
using type = mp_true;
};
template<class T> struct mp_similar_impl<T, T>
{
using type = mp_true;
};
template<class T1, class T2> struct mp_similar_impl<T1, T2>
{
using type = mp_false;
};
template<template<class...> class L, class... T1, class... T2> struct mp_similar_impl<L<T1...>, L<T2...>>
{
using type = mp_true;
};
template<class T1, class T2, class T3, class... T> struct mp_similar_impl<T1, T2, T3, T...>
{
using type = mp_all< typename mp_similar_impl<T1, T2>::type, typename mp_similar_impl<T1, T3>::type, typename mp_similar_impl<T1, T>::type... >;
};
} // namespace detail
template<class... T> using mp_similar = typename detail::mp_similar_impl<T...>::type;
// mp_less<T1, T2>
template<class T1, class T2> using mp_less = mp_bool<(T1::value < 0 && T2::value >= 0) || ((T1::value < T2::value) && !(T1::value >= 0 && T2::value < 0))>;

View File

@@ -143,6 +143,7 @@ run mp_plus.cpp ;
run mp_less.cpp ;
run mp_min.cpp ;
run mp_max.cpp ;
run mp_similar.cpp ;
# map
run mp_map_find.cpp ;

45
test/mp_similar.cpp Normal file
View File

@@ -0,0 +1,45 @@
// Copyright 2019 Peter Dimov.
//
// 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
#include <boost/mp11/function.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
template<class> class X;
template<class...> class Y;
int main()
{
using boost::mp11::mp_similar;
using boost::mp11::mp_true;
using boost::mp11::mp_false;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void, void>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void, void, void>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void, void, void, void>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void, void, void, void, void>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void, int>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void, void, int>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void, void, void, int>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<void, void, void, void, int>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<X<void>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<X<void>, X<int>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<X<void>, X<int>, X<float>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<Y<>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<Y<>, Y<void>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<Y<>, Y<void>, Y<void, void>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_similar<Y<>, Y<void>, Y<void, void>, Y<void, void, void>>, mp_true>));
return boost::report_errors();
}