type_name.cpp 1.6 KB

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