1
0
forked from boostorg/mp11

Merge branch 'develop'

This commit is contained in:
Peter Dimov
2015-07-16 00:46:45 +03:00
8 changed files with 175 additions and 9 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
{ {
@@ -20,7 +22,10 @@ template<class T> struct mp_identity
// mp_inherit // mp_inherit
template<class... T> struct mp_inherit: T... {}; template<class... T> struct mp_inherit: T... {};
// mp_if // mp_if, mp_if_c
namespace detail
{
template<bool C, class T, class E> struct mp_if_c_impl; template<bool C, class T, class E> struct mp_if_c_impl;
template<class T, class E> struct mp_if_c_impl<true, T, E> template<class T, class E> struct mp_if_c_impl<true, T, E>
@@ -33,11 +38,15 @@ template<class T, class E> struct mp_if_c_impl<false, T, E>
using type = E; using type = E;
}; };
template<bool C, class T, class E> using mp_if_c = typename mp_if_c_impl<C, T, E>::type; } // namespace detail
template<class C, class T, class E> using mp_if = typename mp_if_c_impl<static_cast<bool>( C::value ), T, E>::type; template<bool C, class T, class E> using mp_if_c = typename detail::mp_if_c_impl<C, T, E>::type;
template<class C, class T, class E> using mp_if = typename detail::mp_if_c_impl<static_cast<bool>(C::value), T, E>::type;
// mp_eval_if, mp_eval_if_c
namespace detail
{
// mp_eval_if
template<bool C, class T, template<class...> class F, class... U> struct mp_eval_if_c_impl; template<bool C, class T, template<class...> class F, class... U> struct mp_eval_if_c_impl;
template<class T, template<class...> class F, class... U> struct mp_eval_if_c_impl<true, T, F, U...> template<class T, template<class...> class F, class... U> struct mp_eval_if_c_impl<true, T, F, U...>
@@ -50,9 +59,36 @@ template<class T, template<class...> class F, class... U> struct mp_eval_if_c_im
using type = F<U...>; using type = F<U...>;
}; };
template<bool C, class T, template<class...> class F, class... U> using mp_eval_if_c = typename mp_eval_if_c_impl<C, T, F, U...>::type; } // namespace detail
template<class C, class T, template<class...> class F, class... U> using mp_eval_if = typename mp_eval_if_c_impl<static_cast<bool>( C::value ), T, F, U...>::type; template<bool C, class T, template<class...> class F, class... U> using mp_eval_if_c = typename detail::mp_eval_if_c_impl<C, 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
// 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
template<template<class...> class F, class... T> struct mp_defer
{
using type = F<T...>;
};
// 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) ;

View File

@@ -7,8 +7,8 @@
// http://www.boost.org/LICENSE_1_0.txt // http://www.boost.org/LICENSE_1_0.txt
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/mp11/integral.hpp> #include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits> #include <type_traits>
#include <cstddef> #include <cstddef>

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();
}

View File

@@ -7,9 +7,9 @@
// http://www.boost.org/LICENSE_1_0.txt // http://www.boost.org/LICENSE_1_0.txt
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/mp11/utility.hpp> #include <boost/mp11/utility.hpp>
#include <boost/mp11/integral.hpp> #include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits> #include <type_traits>
int main() int main()

View File

@@ -7,9 +7,9 @@
// http://www.boost.org/LICENSE_1_0.txt // http://www.boost.org/LICENSE_1_0.txt
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/mp11/utility.hpp> #include <boost/mp11/utility.hpp>
#include <boost/mp11/integral.hpp> #include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits> #include <type_traits>
int main() int main()

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();
}