1
0
forked from boostorg/mp11

Add mp_value; enable mp_rename on value lists. Refs #53.

This commit is contained in:
Peter Dimov
2023-05-14 19:45:09 +03:00
parent 173e5d8098
commit 570acee866
7 changed files with 139 additions and 4 deletions

View File

@@ -123,6 +123,17 @@
# endif # endif
#endif #endif
// BOOST_MP11_HAS_TEMPLATE_AUTO
#if defined(__cpp_nontype_template_parameter_auto) && __cpp_nontype_template_parameter_auto >= 201606L
# define BOOST_MP11_HAS_TEMPLATE_AUTO
#endif
#if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
// mp_value<0> is bool, mp_value<-1L> is int, etc
# undef BOOST_MP11_HAS_TEMPLATE_AUTO
#endif
// BOOST_MP11_DEPRECATED(msg) // BOOST_MP11_DEPRECATED(msg)
#if BOOST_MP11_WORKAROUND( BOOST_MP11_CLANG, < 304 ) #if BOOST_MP11_WORKAROUND( BOOST_MP11_CLANG, < 304 )

View File

@@ -1,7 +1,7 @@
#ifndef BOOST_MP11_DETAIL_MP_RENAME_HPP_INCLUDED #ifndef BOOST_MP11_DETAIL_MP_RENAME_HPP_INCLUDED
#define BOOST_MP11_DETAIL_MP_RENAME_HPP_INCLUDED #define BOOST_MP11_DETAIL_MP_RENAME_HPP_INCLUDED
// Copyright 2015-2021 Peter Dimov. // Copyright 2015-2023 Peter Dimov.
// //
// Distributed under the Boost Software License, Version 1.0. // Distributed under the Boost Software License, Version 1.0.
// //
@@ -9,6 +9,8 @@
// http://www.boost.org/LICENSE_1_0.txt // http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/detail/mp_defer.hpp> #include <boost/mp11/detail/mp_defer.hpp>
#include <boost/mp11/detail/mp_value.hpp>
#include <boost/mp11/detail/config.hpp>
namespace boost namespace boost
{ {
@@ -19,21 +21,31 @@ namespace mp11
namespace detail namespace detail
{ {
template<class A, template<class...> class B> struct mp_rename_impl template<class L, template<class...> class B> struct mp_rename_impl
{ {
// An error "no type named 'type'" here means that the first argument to mp_rename is not a list // An error "no type named 'type'" here means that the first argument to mp_rename is not a list
}; };
template<template<class...> class A, class... T, template<class...> class B> struct mp_rename_impl<A<T...>, B>: mp_defer<B, T...> template<template<class...> class L, class... T, template<class...> class B> struct mp_rename_impl<L<T...>, B>: mp_defer<B, T...>
{ {
}; };
#if defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
template<template<auto...> class L, auto... A, template<class...> class B> struct mp_rename_impl<L<A...>, B>: mp_defer<B, mp_value<A>...>
{
};
#endif
} // namespace detail } // namespace detail
template<class A, template<class...> class B> using mp_rename = typename detail::mp_rename_impl<A, B>::type; template<class L, template<class...> class B> using mp_rename = typename detail::mp_rename_impl<L, B>::type;
// mp_apply<F, L>
template<template<class...> class F, class L> using mp_apply = typename detail::mp_rename_impl<L, F>::type; template<template<class...> class F, class L> using mp_apply = typename detail::mp_rename_impl<L, F>::type;
// mp_apply_q<Q, L>
template<class Q, class L> using mp_apply_q = typename detail::mp_rename_impl<L, Q::template fn>::type; template<class Q, class L> using mp_apply_q = typename detail::mp_rename_impl<L, Q::template fn>::type;
} // namespace mp11 } // namespace mp11

View File

@@ -0,0 +1,25 @@
#ifndef BOOST_MP11_DETAIL_MP_VALUE_HPP_INCLUDED
#define BOOST_MP11_DETAIL_MP_VALUE_HPP_INCLUDED
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/detail/config.hpp>
#include <type_traits>
#if defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
namespace boost
{
namespace mp11
{
template<auto A> using mp_value = std::integral_constant<decltype(A), A>;
} // namespace mp11
} // namespace boost
#endif // #if defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
#endif // #ifndef BOOST_MP11_DETAIL_MP_VALUE_HPP_INCLUDED

View File

@@ -9,6 +9,7 @@
// http://www.boost.org/LICENSE_1_0.txt // http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/version.hpp> #include <boost/mp11/version.hpp>
#include <boost/mp11/detail/mp_value.hpp>
#include <type_traits> #include <type_traits>
#include <cstddef> #include <cstddef>

View File

@@ -34,6 +34,7 @@ run mp_third.cpp ;
run mp_push_front.cpp ; run mp_push_front.cpp ;
run mp_push_back.cpp ; run mp_push_back.cpp ;
run mp_rename.cpp ; run mp_rename.cpp ;
run mp_rename_2.cpp ;
run mp_append.cpp ; run mp_append.cpp ;
run mp_append_2.cpp ; run mp_append_2.cpp ;
run mp_append_sf.cpp ; run mp_append_sf.cpp ;
@@ -133,6 +134,7 @@ run mp_join.cpp ;
# integral # integral
run integral.cpp ; run integral.cpp ;
run mp_value.cpp ;
# utility # utility
run mp_identity.cpp ; run mp_identity.cpp ;

44
test/mp_rename_2.cpp Normal file
View File

@@ -0,0 +1,44 @@
// Copyright 2015-2023 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/list.hpp>
#if !defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
#pragma message("Test skipped because BOOST_MP11_HAS_TEMPLATE_AUTO is not defined")
int main() {}
#else
#include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<auto... T> struct L {};
int main()
{
using boost::mp11::mp_list;
using boost::mp11::mp_rename;
using boost::mp11::mp_true;
using boost::mp11::mp_false;
using boost::mp11::mp_int;
using boost::mp11::mp_size_t;
BOOST_TEST_TRAIT_SAME(mp_rename<L<>, mp_list>, mp_list<>);
BOOST_TEST_TRAIT_SAME(mp_rename<L<false>, mp_list>, mp_list<mp_false>);
BOOST_TEST_TRAIT_SAME(mp_rename<L<true>, mp_list>, mp_list<mp_true>);
BOOST_TEST_TRAIT_SAME(mp_rename<L<0>, mp_list>, mp_list<mp_int<0>>);
BOOST_TEST_TRAIT_SAME(mp_rename<L<-1>, mp_list>, mp_list<mp_int<-1>>);
BOOST_TEST_TRAIT_SAME(mp_rename<L<std::size_t(0)>, mp_list>, mp_list<mp_size_t<0>>);
BOOST_TEST_TRAIT_SAME(mp_rename<L<std::size_t(1)>, mp_list>, mp_list<mp_size_t<1>>);
BOOST_TEST_TRAIT_SAME(mp_rename<L<false, 0, std::size_t(0)>, mp_list>, mp_list<mp_false, mp_int<0>, mp_size_t<0>>);
return boost::report_errors();
}
#endif

40
test/mp_value.cpp Normal file
View File

@@ -0,0 +1,40 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/integral.hpp>
#if !defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
#pragma message("Test skipped because BOOST_MP11_HAS_TEMPLATE_AUTO is not defined")
int main() {}
#else
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <cstddef>
int main()
{
using boost::mp11::mp_value;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<false>, std::integral_constant<bool, false>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<true>, std::integral_constant<bool, true>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<'a'>, std::integral_constant<char, 'a'>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<'0'>, std::integral_constant<char, '0'>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<0>, std::integral_constant<int, 0>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<-1>, std::integral_constant<int, -1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<0L>, std::integral_constant<long, 0>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<-1L>, std::integral_constant<long, -1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<0u>, std::integral_constant<unsigned, 0>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_value<1u>, std::integral_constant<unsigned, 1>>));
return boost::report_errors();
}
#endif