summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/type/metafunction_class.cpp
blob: b0123708b10f6bae14024420794350ecb5977078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
#include <boost/hana/concept/metafunction.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/not.hpp>
#include <boost/hana/type.hpp>

#include <type_traits>
namespace hana = boost::hana;


struct x1; struct x2; struct x3;
struct y1 { }; struct y2 { }; struct y3 { };
struct f { template <typename ...> struct apply { struct type; }; };

template <typename F, typename ...T>
constexpr auto valid_call(F f, T ...t) -> decltype(((void)f(t...)), true)
{ return true; }
constexpr auto valid_call(...)
{ return false; }

BOOST_HANA_CONSTANT_CHECK(hana::equal(
    hana::metafunction_class<f>(),
    hana::type_c<f::apply<>::type>
));
BOOST_HANA_CONSTANT_CHECK(hana::equal(
    hana::metafunction_class<f>(hana::type_c<x1>),
    hana::type_c<f::apply<x1>::type>
));
BOOST_HANA_CONSTANT_CHECK(hana::equal(
    hana::metafunction_class<f>(hana::type_c<x1>, hana::type_c<x2>),
    hana::type_c<f::apply<x1, x2>::type>
));
BOOST_HANA_CONSTANT_CHECK(hana::equal(
    hana::metafunction_class<f>(hana::type_c<x1>, hana::type_c<x2>, hana::type_c<x3>),
    hana::type_c<f::apply<x1, x2, x3>::type>
));

using F = decltype(hana::metafunction_class<f>);
static_assert(std::is_same<F::apply<>, f::apply<>>{}, "");
static_assert(std::is_same<F::apply<x1>, f::apply<x1>>{}, "");
static_assert(std::is_same<F::apply<x1, x2>, f::apply<x1, x2>>{}, "");
static_assert(std::is_same<F::apply<x1, x2, x3>, f::apply<x1, x2, x3>>{}, "");

// Make sure we're SFINAE-friendly
struct no_type { template <typename ...> struct apply { }; };
static_assert(!valid_call(hana::metafunction_class<no_type>), "");
static_assert(!valid_call(hana::metafunction_class<no_type>, hana::type_c<x1>), "");

// Make sure we model the Metafunction concept
static_assert(hana::Metafunction<decltype(hana::metafunction_class<f>)>::value, "");
static_assert(hana::Metafunction<decltype(hana::metafunction_class<f>)&>::value, "");

// Make sure metafunction_class is SFINAE-friendly
struct not_a_mfc1 { template <typename ...> struct apply { }; };
struct not_a_mfc2 { };
BOOST_HANA_CONSTANT_CHECK(hana::not_(
    hana::is_valid(hana::metafunction_class<not_a_mfc1>)(hana::type_c<void>)
));
BOOST_HANA_CONSTANT_CHECK(hana::not_(
    hana::is_valid(hana::metafunction_class<not_a_mfc2>)(hana::type_c<void>)
));


// Make sure we don't read from a non-constexpr variable
int main() {
    auto t = hana::type_c<x1>;
    constexpr auto r = hana::metafunction_class<f>(t);
    (void)r;
}