An open API service indexing awesome lists of open source software.

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

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"
}
````