1
0
forked from boostorg/mp11

Add mp_valid, mp_defer, mp_defer_if_valid.

This commit is contained in:
Peter Dimov
2015-07-16 00:40:40 +03:00
parent a5dd767f93
commit 61b897e857
5 changed files with 154 additions and 0 deletions

View File

@@ -8,6 +8,8 @@
// See accompanying file LICENSE_1_0.txt or copy at // See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt // http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/integral.hpp>
namespace boost namespace boost
{ {
@@ -63,8 +65,30 @@ template<bool C, class T, template<class...> class F, class... U> using mp_eval_
template<class C, class T, template<class...> class F, class... U> using mp_eval_if = typename detail::mp_eval_if_c_impl<static_cast<bool>(C::value), T, F, U...>::type; template<class C, class T, template<class...> class F, class... U> using mp_eval_if = typename detail::mp_eval_if_c_impl<static_cast<bool>(C::value), T, F, U...>::type;
// mp_valid // mp_valid
// implementation by Bruno Dutra (by the name is_evaluable)
namespace detail
{
template<template<class...> class F, class... T> struct mp_valid_impl
{
template<template<class...> class G, class = G<T...>> static mp_true check(int);
template<template<class...> class> static mp_false check(...);
using type = decltype(check<F>(0));
};
} // namespace detail
template<template<class...> class F, class... T> using mp_valid = typename detail::mp_valid_impl<F, T...>::type;
// mp_defer // mp_defer
template<template<class...> class F, class... T> struct mp_defer
{
using type = F<T...>;
};
// mp_defer_if_valid // mp_defer_if_valid
template<template<class...> class F, class... T> using mp_defer_if_valid = mp_if<mp_valid<F, T...>, mp_defer<F, T...>, mp_inherit<>>;
} // namespace boost } // namespace boost

View File

@@ -57,6 +57,9 @@ run mp_identity.cpp : : : $(REQ) ;
run mp_inherit.cpp : : : $(REQ) ; run mp_inherit.cpp : : : $(REQ) ;
run mp_if.cpp : : : $(REQ) ; run mp_if.cpp : : : $(REQ) ;
run mp_eval_if.cpp : : : $(REQ) ; run mp_eval_if.cpp : : : $(REQ) ;
run mp_valid.cpp : : : $(REQ) ;
run mp_defer.cpp : : : $(REQ) ;
run mp_defer_if_valid.cpp : : : $(REQ) ;
# integer_sequence # integer_sequence
run integer_sequence.cpp : : : $(REQ) ; run integer_sequence.cpp : : : $(REQ) ;

26
test/mp_defer.cpp Normal file
View File

@@ -0,0 +1,26 @@
// Copyright 2015 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/utility.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
template<class T> using add_pointer = T*;
using boost::mp_defer;
template<class T> using add_pointer_impl = mp_defer<add_pointer, T>;
int main()
{
BOOST_TEST_TRAIT_TRUE((std::is_same<add_pointer_impl<void>::type, void*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<add_pointer_impl<int>::type, int*>));
return boost::report_errors();
}

View File

@@ -0,0 +1,29 @@
// Copyright 2015 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/utility.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
using boost::mp_defer_if_valid;
using boost::mp_identity;
template<class... T> using mp_identity_2 = typename mp_defer_if_valid<mp_identity, T...>::type;
int main()
{
using boost::mp_valid;
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_identity_2>));
BOOST_TEST_TRAIT_TRUE((mp_valid<mp_identity_2, void>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_identity_2<void>, mp_identity<void>>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_identity_2, void, void>));
return boost::report_errors();
}

72
test/mp_valid.cpp Normal file
View File

@@ -0,0 +1,72 @@
// Copyright 2015 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/utility.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T> struct Xi
{
};
template<> struct Xi<void>
{
using type = void;
};
template<class T> using X = typename Xi<T>::type;
template<class T> using add_pointer = T*;
template<class T> using add_reference = T&;
template<class T> using add_extents = T[];
int main()
{
using boost::mp_valid;
using boost::mp_identity;
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_identity>));
BOOST_TEST_TRAIT_TRUE((mp_valid<mp_identity, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_identity, void, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<X>));
BOOST_TEST_TRAIT_TRUE((mp_valid<X, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<X, int>));
BOOST_TEST_TRAIT_FALSE((mp_valid<X, void, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<add_pointer>));
BOOST_TEST_TRAIT_TRUE((mp_valid<add_pointer, void>));
BOOST_TEST_TRAIT_TRUE((mp_valid<add_pointer, int>));
#if !defined( BOOST_MSVC ) || !BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
// msvc-12.0 can form pointer to reference
BOOST_TEST_TRAIT_FALSE((mp_valid<add_pointer, int&>));
#endif
BOOST_TEST_TRAIT_FALSE((mp_valid<add_pointer, void, void>));
#if !defined( BOOST_GCC ) || !BOOST_WORKAROUND( BOOST_GCC, <= 40902 )
// g++ 4.9.2 doesn't like add_reference for some reason or other
BOOST_TEST_TRAIT_FALSE((mp_valid<add_reference>));
#if !defined( BOOST_MSVC ) || !BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
// msvc-12.0 gives an internal error here
BOOST_TEST_TRAIT_FALSE((mp_valid<add_reference, void>));
#endif
BOOST_TEST_TRAIT_TRUE((mp_valid<add_reference, int>));
BOOST_TEST_TRAIT_FALSE((mp_valid<add_reference, int, int>));
#endif
BOOST_TEST_TRAIT_FALSE((mp_valid<add_extents>));
BOOST_TEST_TRAIT_TRUE((mp_valid<add_extents, int>));
#if !defined( BOOST_MSVC ) || !BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
// msvc-12.0 can form arrays to void or int&
BOOST_TEST_TRAIT_FALSE((mp_valid<add_extents, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<add_extents, int&>));
#endif
BOOST_TEST_TRAIT_FALSE((mp_valid<add_extents, int, int>));
return boost::report_errors();
}