General Utility Library for C++17 25.4.1
type_name.h
Go to the documentation of this file.
1
26#ifndef GUL17_TYPE_NAME_H_
27#define GUL17_TYPE_NAME_H_
28
29#include <string_view>
30
31#include "gul17/internal.h"
32
33namespace gul17 {
34
66template <typename T>
68
92template <class T>
93constexpr std::string_view type_name()
94{
95#if defined(__GNUC__)
96 // Clang returns something like "return_type function_name() [T = template_parameter; ...]"
97 // GCC returns something like "return_type function_name() [with T = template_parameter]"
98 auto s = std::string_view{ static_cast<const char*>(__PRETTY_FUNCTION__) };
99 auto const start_idx = s.find(" = ") + 3; // len(" = ") == 3
100 s.remove_prefix(start_idx);
101
102 auto const colon_idx = s.find(";");
103 auto const suff_length =
104 (colon_idx != std::string_view::npos) ? s.length() - colon_idx : 1; // len("]") == 1
105 s.remove_suffix(suff_length);
106 return s;
107#elif defined(_MSC_VER)
108 // MSVC returns something like "return_type function_name<template_parameter>()"
109 auto s = std::string_view{ __FUNCSIG__ };
110 auto const start_idx = s.find("gul17::type_name<") + sizeof("gul17::type_name<") - 1;
111 s.remove_prefix(start_idx);
112
113 for (auto end_idx = s.length() - 1; end_idx; --end_idx) {
114 if (s[end_idx] != '>')
115 continue;
116 s.remove_suffix(s.length() - end_idx);
117 break;
118 }
119 return s;
120#else
121 return "";
122#endif
123}
124
126
127} // namespace gul17
128
129#endif
130
131// vi:ts=4:sw=4:et:sts=4
A helper class to debug types.
Definition type_name.h:67
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:121
constexpr std::string_view type_name()
Generate a human readable string describing a type.
Definition type_name.h:93
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:26