scl-utility

Type name utilities

Compile-time extraction of human-readable type names without RTTI.

Contents:


type_name

Retrieves the fully qualified name of a template type T at compile-time as a std::string_view.

This utility leverages compiler-specific macros to capture the decorated function signature and extract the exact type representation.

Semantics

Examples

#include <scl/utility/meta/type.h>
#include <vector>
#include <string>

using ::scl::type_name;

struct MyStruct {};

// Fundamental types
static_assert(type_name<int>() == "int");

// Standard library types
// Note: exact string may vary slightly by STL implementation
static_assert(type_name<std::string>().find("basic_string") != std::string_view::npos);

// User-defined types
// GCC/Clang: "MyStruct"  |  MSVC: "struct MyStruct"
static_assert(type_name<MyStruct>().find("MyStruct") != std::string_view::npos);

// Templates
static_assert(type_name<std::vector<MyStruct>>().find("vector<MyStruct>") != std::string_view::npos);

Typical Use Cases

Generating human-readable logs, error messages in generic code, or implementing lightweight reflection systems where type identities need to be displayed or compared as strings without the overhead of typeid(T).name().


type_short_name

Retrieves only the terminal identifier of the type T, stripping all namespace and class qualifiers.

Description

This function processes the result of type_name<T>() and removes everything up to the last :: delimiter. Additionally, struct/class/union/enum prefixes (present on MSVC) and template arguments are stripped, so the result is always the bare identifier.

Example

#include <scl/utility/meta/type.h>

namespace app::core { struct Task {}; }

int main() {
    constexpr auto full    = scl::type_name<app::core::Task>();       // "app::core::Task" (GCC/Clang)
    constexpr auto short_n = scl::type_short_name<app::core::Task>(); // "Task"
}