General Utility Library for C++14  2.11
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 
65 template <typename T>
67 
91 template <class T>
93 {
94 #if defined(__GNUC__)
95  // Clang returns something like "return_type function_name() [T = template_parameter; ...]"
96  // GCC returns something like "return_type function_name() [with T = template_parameter]"
97  auto s = string_view{ static_cast<const char*>(__PRETTY_FUNCTION__) };
98  auto const start_idx = s.find(" = ") + 3; // len(" = ") == 3
99  s.remove_prefix(start_idx);
100 
101  auto const colon_idx = s.find(";");
102  auto const suff_length =
103  (colon_idx != string_view::npos) ? s.length() - colon_idx : 1; // len("]") == 1
104  s.remove_suffix(suff_length);
105  return s;
106 #elif defined(_MSC_VER)
107  // MSVC returns something like "return_type function_name<template_parameter>()"
108  auto s = string_view{ __FUNCSIG__ };
109  auto const start_idx = s.find("gul14::type_name<") + sizeof("gul14::type_name<") - 1;
110  s.remove_prefix(start_idx);
111 
112  for (auto end_idx = s.length() - 1; end_idx; --end_idx) {
113  if (s[end_idx] != '>')
114  continue;
115  s.remove_suffix(s.length() - end_idx);
116  break;
117  }
118  return s;
119 #else
120  return "";
121 #endif
122 }
123 
125 
126 } // namespace gul14
127 
128 #endif
129 
130 // vi:ts=4:sw=4:et:sts=4
A helper class to debug types.
Definition: type_name.h:66
constexpr string_view type_name()
Generate a human readable string describing a type.
Definition: type_name.h:92
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
Provides a gul14::string_view that is fully compatible with C++17's std::string_view.