StaticTypeInfo
🏀 Up your type-game. A small C++ library for compile-time type names and type indices.
Install / Use
/learn @TheLartians/StaticTypeInfoREADME
StaticTypeInfo
A simple C++17 compile-time type info library.
API
The header-only library exposes two main constexpr functions: constexpr TypeName getTypeName<T>() and constexpr TypeIndex getTypeIndex<T>().
The TypeIndex may be used as a key in any hash-based container.
#include <static_type_info.h>
void example() {
using namespace static_type_info;
// compile-time type names
constexpr auto name = getTypeName<int>();
static_assert(name == "int");
// compile-time type indices
constexpr auto intIdx = getTypeIndex<int>();
constexpr auto floatIdx = getTypeIndex<float>();
static_assert(intIdx != floatIdx);
// combine both in a single type
constexpr auto typeID = getTypeID<int>();
static_assert(typeID.name == "int");
static_assert(typeID.index == getTypeIndex<int>());
}
Compatibility
The library has been tested with AppleClang 11, Visual Studio 16 2019, and gcc-9. Note that type names and indices are not compatible between compilers.
How it works
The type name is extracted from the macro __PRETTY_FUNCTION__ (clang/gcc) or __FUNCSIG__ (on MSVC) inside a probe function and converted to a string_view using the constexpr constructor.
The type index is determined from the pointer to a static member of a templated string (clang / msvc) or from a 64 bit fnv1a hash of the type name (gcc and others).
How to integrate
Use CPM.cmake to easily add the headers to your CMake project.
CPMAddPackage(
NAME StaticTypeInfo
VERSION 1.3
GIT_REPOSITORY https://github.com/TheLartians/StaticTypeInfo
)
target_link_libraries(myProject StaticTypeInfo)
Licence
The library is released under the MIT license. See the LICENSE for more info.
Related Skills
node-connect
348.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
348.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
348.5kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
