• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/equal.hpp>
7 #include <boost/hana/hash.hpp>
8 #include <boost/hana/integral_constant.hpp>
9 #include <boost/hana/type.hpp>
10 
11 #include <type_traits>
12 #include <utility>
13 namespace hana = boost::hana;
14 
15 
16 // Sample implementation of a compile-time set data structure. Of course,
17 // this naive implementation only works when no two elements of the same
18 // set have the same hash.
19 template <typename T>
20 struct bucket { };
21 
22 template <typename ...T>
23 struct set
24     : bucket<typename decltype(hana::hash(std::declval<T>()))::type>...
25 { };
26 
27 template <typename Set, typename T>
28 struct contains
29     : std::is_base_of<
30         bucket<typename decltype(hana::hash(std::declval<T>()))::type>,
31         Set
32     >
33 { };
34 
35 using Set = set<hana::int_<1>, hana::ulong<2>, hana::type<char>>;
36 
37 static_assert(contains<Set, hana::int_<1>>{}, "");
38 static_assert(contains<Set, hana::ulong<2>>{}, "");
39 static_assert(contains<Set, hana::type<char>>{}, "");
40 
41 static_assert(!contains<Set, hana::int_<3>>{}, "");
42 static_assert(!contains<Set, hana::type<float>>{}, "");
43 
main()44 int main() { }
45