Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / hana / test / type / metafunction_class.cpp
1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4
5 #include <boost/hana/assert.hpp>
6 #include <boost/hana/concept/metafunction.hpp>
7 #include <boost/hana/equal.hpp>
8 #include <boost/hana/type.hpp>
9
10 #include <type_traits>
11 namespace hana = boost::hana;
12
13
14 struct x1; struct x2; struct x3;
15 struct y1 { }; struct y2 { }; struct y3 { };
16 struct f { template <typename ...> struct apply { struct type; }; };
17
18 template <typename F, typename ...T>
19 constexpr auto valid_call(F f, T ...t) -> decltype(((void)f(t...)), true)
20 { return true; }
21 constexpr auto valid_call(...)
22 { return false; }
23
24 BOOST_HANA_CONSTANT_CHECK(hana::equal(
25     hana::metafunction_class<f>(),
26     hana::type_c<f::apply<>::type>
27 ));
28 BOOST_HANA_CONSTANT_CHECK(hana::equal(
29     hana::metafunction_class<f>(hana::type_c<x1>),
30     hana::type_c<f::apply<x1>::type>
31 ));
32 BOOST_HANA_CONSTANT_CHECK(hana::equal(
33     hana::metafunction_class<f>(hana::type_c<x1>, hana::type_c<x2>),
34     hana::type_c<f::apply<x1, x2>::type>
35 ));
36 BOOST_HANA_CONSTANT_CHECK(hana::equal(
37     hana::metafunction_class<f>(hana::type_c<x1>, hana::type_c<x2>, hana::type_c<x3>),
38     hana::type_c<f::apply<x1, x2, x3>::type>
39 ));
40
41 using F = decltype(hana::metafunction_class<f>);
42 static_assert(std::is_same<F::apply<>, f::apply<>>{}, "");
43 static_assert(std::is_same<F::apply<x1>, f::apply<x1>>{}, "");
44 static_assert(std::is_same<F::apply<x1, x2>, f::apply<x1, x2>>{}, "");
45 static_assert(std::is_same<F::apply<x1, x2, x3>, f::apply<x1, x2, x3>>{}, "");
46
47 // Make sure we're SFINAE-friendly
48 struct no_type { template <typename ...> struct apply { }; };
49 static_assert(!valid_call(hana::metafunction_class<no_type>), "");
50 static_assert(!valid_call(hana::metafunction_class<no_type>, hana::type_c<x1>), "");
51
52 // Make sure we model the Metafunction concept
53 static_assert(hana::Metafunction<decltype(hana::metafunction_class<f>)>::value, "");
54 static_assert(hana::Metafunction<decltype(hana::metafunction_class<f>)&>::value, "");
55
56
57 // Make sure we don't read from a non-constexpr variable
58 int main() {
59     auto t = hana::type_c<x1>;
60     constexpr auto r = hana::metafunction_class<f>(t);
61     (void)r;
62 }