• 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/experimental/type_name.hpp>
8 #include <boost/hana/string.hpp>
9 
10 #include <cstdlib>
11 #include <iostream>
12 #include <regex>
13 #include <string>
14 namespace hana = boost::hana;
15 
16 
17 template <typename ...T>
18 struct Template { };
19 
20 template <typename T>
check_matches(std::string const & re)21 void check_matches(std::string const& re) {
22     std::string name = hana::to<char const*>(hana::experimental::type_name<T>());
23     std::regex regex{re};
24     if (!std::regex_match(name, regex)) {
25         std::cerr << "type name '" << name << "' does not match regex '" << re << "'" << std::endl;
26         std::abort();
27     }
28 }
29 
main()30 int main() {
31     // Make sure this can be obtained at compile-time
32     BOOST_HANA_CONSTANT_CHECK(hana::equal(
33         hana::experimental::type_name<void>(),
34         BOOST_HANA_STRING("void")
35     ));
36 
37     BOOST_HANA_CONSTANT_CHECK(hana::equal(
38         hana::experimental::type_name<int>(),
39         BOOST_HANA_STRING("int")
40     ));
41 
42     // Make sure we get something reasonable
43     check_matches<int const>("int const|const int");
44     check_matches<int&>(R"(int\s*&)");
45     check_matches<int const&>(R"(const\s+int\s*&|int\s+const\s*&)");
46     check_matches<int(&)[]>(R"(int\s*\(\s*&\s*\)\s*\[\s*\])");
47     check_matches<int(&)[10]>(R"(int\s*\(\s*&\s*\)\s*\[\s*10\s*\])");
48     check_matches<Template<void, char const*>>(R"(Template<\s*void\s*,\s*(char const|const char)\s*\*\s*>)");
49     check_matches<void(*)(int)>(R"(void\s*\(\s*\*\s*\)\s*\(\s*int\s*\))");
50 }
51