General Utility Library for C++14  2.7
type_name.h
Go to the documentation of this file.
1 
26 #ifndef GUL14_TYPE_NAME_H_
27 #define GUL14_TYPE_NAME_H_
28 
29 #include "gul14/internal.h"
30 #include "gul14/string_view.h"
31 
32 namespace gul14 {
33 
59 template <typename T>
61 
85 template <class T>
87 {
88 #if defined(__GNUC__)
89  // Clang returns something like "return_type function_name() [T = template_parameter; ...]"
90  // GCC returns something like "return_type function_name() [with T = template_parameter]"
91  auto s = string_view{ static_cast<const char*>(__PRETTY_FUNCTION__) };
92  auto const start_idx = s.find(" = ") + 3; // len(" = ") == 3
93  s.remove_prefix(start_idx);
94 
95  auto const colon_idx = s.find(";");
96  auto const suff_length =
97  (colon_idx != string_view::npos) ? s.length() - colon_idx : 1; // len("]") == 1
98  s.remove_suffix(suff_length);
99  return s;
100 #elif defined(_MSC_VER)
101  // MSVC returns something like "return_type function_name<template_parameter>()"
102  auto s = string_view{ __FUNCSIG__ };
103  auto const start_idx = s.find("gul14::type_name<") + sizeof("gul14::type_name<") - 1;
104  s.remove_prefix(start_idx);
105 
106  for (auto end_idx = s.length() - 1; end_idx; --end_idx) {
107  if (s[end_idx] != '>')
108  continue;
109  s.remove_suffix(s.length() - end_idx);
110  break;
111  }
112  return s;
113 #else
114  return "";
115 #endif
116 }
117 
118 } // namespace gul14
119 
120 #endif
121 
122 // vi:ts=4:sw=4:et:sts=4
gul14::FailToInstantiate
A helper class to debug types.
Definition: type_name.h:60
gul14::basic_string_view< char >
string_view.h
Provides a gul14::string_view that is fully compatible with C++17's std::string_view.
internal.h
Definition of macros used internally by GUL.
gul14
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
gul14::type_name
constexpr string_view type_name()
Generate a human readable string describing a type.
Definition: type_name.h:86