https://github.com/mguludag/typename
A small C++ utility for get type names constexpr from compiler
https://github.com/mguludag/typename
constexpr cpp17 cpp20 cpp23 reflection
Last synced: 4 months ago
JSON representation
A small C++ utility for get type names constexpr from compiler
- Host: GitHub
- URL: https://github.com/mguludag/typename
- Owner: mguludag
- License: mit
- Created: 2024-01-20T17:54:58.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-18T09:45:53.000Z (about 1 year ago)
- Last Synced: 2025-01-03T08:19:36.776Z (6 months ago)
- Topics: constexpr, cpp17, cpp20, cpp23, reflection
- Language: C++
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeName
A small C++17 utility for get type names constexpr from compiler## [Usage](https://godbolt.org/z/GYa4Ys3jh)
```C++
#include "TypeName.hpp"
#include
#include// create variadic template function
template
auto printTypes(Args&&... args) -> void {
// NOLINTNEXTLINE [readability-identifier-length]
auto c = 0U;
std::print("types are: "),
// NOLINTNEXTLINE [cppcoreguidelines-pro-bounds-array-to-pointer-decay]
(std::print("{}{}", TypeUtils::Type::name(std::forward(args)),
(++c == sizeof...(args) ? "\n" : ", ")),
...); // NOLINT
}// you can able to specialize TypeUtils::Type::name<> for desired output per type
template <>
auto constexpr TypeUtils::Type::name() noexcept
-> std::string_view {
return "std::string_view";
}auto main() -> int
{
auto constexpr name = TypeUtils::Type::name(TypeUtils::Type{});
printTypes(5, 5.5, "aaa", 'a', TypeUtils::Type{}, name, std::nullopt);
// prints: "types are: int, double, const char*, char, TypeUtils::Type, std::string_view, std::nullopt_t"
}
````