From 3036b4eb58bc7d6ffb510369236a27c432f0cf9f Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Sun, 29 Nov 2026 10:33:59 -0800 Subject: [PATCH] feat(reflection): add public rfl::type_name() and rfl::type_name_v Exposes `rfl::type_name()` and `rfl::type_name_v` via a thin, self-contained public header (``, mirroring `` from #451/#465) returning a compile-time `std::string_view` directly from `rfl::internal::get_type_name_str_view()` without instantiating `rfl::Literal` or `rfl::internal::StringLiteral`. Parent: depends on `fix(reflection): fix dangling string_view in rfl::internal::get_type_name_str_view` (open upstream PR #804). Judge polish applied: - Drops redundant `rfl::num_fields_v` (`rfl::num_fields` is already a public variable template since #465). - Uses #804's `internal::get_type_name_str_view()` directly to avoid `StringLiteral` / `make_index_sequence` bloat. Precedent: https://github.com/munich-data-engineering/reflect-cpp/issues/451 Downstream receipts: - NVIDIA/cuda-quantum calls private `rfl::internal::get_type_name_str_view()` with comment "Unfortunately, this has to use a private API of reflect-cpp": https://github.com/NVIDIA/cuda-quantum/blob/81ef20d9d4a1e0912a6dbc68637ece017e8fd324/cudaq/lib/Target/Reflection.h#L27-L31 - munich-data-engineering/sqlgen hand-rolled `sqlgen::internal::get_type_name_str_view`: https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/include/sqlgen/internal/get_type_name.hpp#L13-L44 - games-on-whales/wolf instantiates `rfl::type_name_t<...>().str()`: https://github.com/games-on-whales/wolf/blob/ac2461f68feda41fdc7a7c8e812b9ab4b80520b7/src/moonlight-server/api/api.cpp#L26 --- include/rfl.hpp | 1 + include/rfl/type_name.hpp | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 include/rfl/type_name.hpp diff --git a/include/rfl.hpp b/include/rfl.hpp index 432fff2f6..ab48b0509 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -92,6 +92,7 @@ #include "rfl/to_named_tuple.hpp" #include "rfl/to_view.hpp" #include "rfl/tuple_cat.hpp" +#include "rfl/type_name.hpp" #include "rfl/type_name_t.hpp" #include "rfl/view_t.hpp" #include "rfl/visit.hpp" diff --git a/include/rfl/type_name.hpp b/include/rfl/type_name.hpp new file mode 100644 index 000000000..b5f594cae --- /dev/null +++ b/include/rfl/type_name.hpp @@ -0,0 +1,27 @@ +#ifndef RFL_TYPE_NAME_HPP_ +#define RFL_TYPE_NAME_HPP_ + +#include + +#include "internal/get_type_name.hpp" + +namespace rfl { + +/// Compile-time `std::string_view` containing the name of type `T` without +/// instantiating an `rfl::Literal` or `rfl::internal::StringLiteral`. +/// @tparam T The type whose name to retrieve +template +inline constexpr std::string_view type_name_v = + internal::get_type_name_str_view(); + +/// Returns the compile-time name of type `T` as a `std::string_view` (usable +/// in both `constexpr` and runtime contexts). +/// @tparam T The type whose name to retrieve +template +constexpr std::string_view type_name() noexcept { + return type_name_v; +} + +} // namespace rfl + +#endif