NAMEOFobtains simple name of variable, function, macro.NAMEOF_FULLobtains full name of variable, function, macro.NAMEOF_RAWobtains raw expression text.NAMEOF_ENUMobtains name of enum value.NAMEOF_ENUM_ORobtains enum name or fallback.NAMEOF_ENUM_CONSTobtains name of enum value at compile time.NAMEOF_ENUM_FLAGobtains name of enum flag value.NAMEOF_TYPEobtains type name.NAMEOF_FULL_TYPEobtains full type name.NAMEOF_SHORT_TYPEobtains short type name.NAMEOF_TYPE_EXPRobtains type name of expression.NAMEOF_FULL_TYPE_EXPRobtains full type name of expression.NAMEOF_SHORT_TYPE_EXPRobtains short type name of expression.NAMEOF_TYPE_RTTIobtains type name using RTTI.NAMEOF_FULL_TYPE_RTTIobtains full type name using RTTI.NAMEOF_SHORT_TYPE_RTTIobtains short type name using RTTI.NAMEOF_MEMBERobtains name of member.NAMEOF_POINTERobtains name of a function, global variable, or static data member.
-
Before use, read the limitations.
-
Check compiler support with a macro or constexpr constant:
API Macro Constant in nameofEnum NAMEOF_ENUM_SUPPORTEDis_nameof_enum_supportedType NAMEOF_TYPE_SUPPORTEDis_nameof_type_supportedRTTI type NAMEOF_TYPE_RTTI_SUPPORTEDis_nameof_type_rtti_supportedMember NAMEOF_MEMBER_SUPPORTEDis_nameof_member_supportedPointer NAMEOF_POINTER_SUPPORTEDis_nameof_pointer_supportedUnsupported APIs fail to compile when used. Define
NAMEOF_ENUM_NO_CHECK_SUPPORTto disable enum support checks, orNAMEOF_TYPE_NO_CHECK_SUPPORTfor the other APIs above. -
NAMEOF,NAMEOF_FULLandNAMEOF_RAWreturnnameof::cstring<N>, a constexpr null-terminated string. Save it before taking a view orc_str():constexpr auto name = NAMEOF(x);. Compile-time names of enum values, types, members and pointers arenameof::name_view, a view of a static name. Both havec_str(),str(),find,rfind,starts_with,ends_with, comparisons andoperator<<, and convert tostring_view.name_view::substrreturns astring_viewand requirespos <= size()(checked withNAMEOF_ASSERT).cstringhas nosubstr. -
In C++20 a
name_viewcan be a template argument:template <nameof::name_view Name> struct TagandTag<NAMEOF_TYPE(int)>. See the limitations. -
Specialize
enum_name,type_name,member_name, orpointer_nameinnameof::customizeto provide custom names. An empty name selects the default. See the enum and type example. -
type_nameuses the type without cv/ref qualifiers, exceptNAMEOF_FULL_TYPE, which uses the exact type.NAMEOF_SHORT_TYPEkeeps the custom name's last identifier. RTTI APIs ignore custom names. -
Specialize
member_nameandpointer_namefor the exact pointer value, including typed null pointers.struct Item { int value; }; inline int global_value = 0; template <> constexpr nameof::string_view nameof::customize::member_name<&Item::value>() noexcept { return "Value"; } template <> constexpr nameof::string_view nameof::customize::pointer_name<&global_value>() noexcept { return "Global"; } -
Enum APIs use C++26 standard reflection when available. Define
NAMEOF_FORCE_COMPILER_SPECIFIC_REFLECTIONbefore includingnameof.hppto keep compiler-specific reflection, orNAMEOF_REQUIRE_STD_REFLECTIONto fail compilation without standard reflection. -
To change the string types, use these macros:
#include <my_lib/string.hpp> #include <my_lib/string_view.hpp> #define NAMEOF_USING_ALIAS_STRING using string = my_lib::String; #define NAMEOF_USING_ALIAS_STRING_VIEW using string_view = my_lib::StringView; #include <nameof.hpp>Aliases must support the
std::stringandstd::string_viewoperations used by nameof. See the minimal interface. -
cstringandname_viewsupportstd::format(C++20 and newer) andfmt. WithNAMEOF_NO_STD_FORMAT,<format>is not included: pass astring_viewtostd::format. Withfmt9, include<fmt/format.h>before<nameof.hpp>to formatcstring.NAMEOF_NO_FMT_FORMATremoves thefmtsupport, sofmt10 and older can use your ownfmt::formatter.fmt11 and newer ignore it and format names as strings. -
NAMEOF_ASSERTchecks preconditions, such as the index ofoperator[], and defaults toassert. Define your ownNAMEOF_ASSERT(...)(an expression valid in aconstexprfunction) or disable checks withNAMEOF_NO_ASSERT. -
Use
NAMEOF_CONFIG_FILEto keep options in one header. It is included before any option is read:#define NAMEOF_CONFIG_FILE "my_nameof_config.hpp" #include <nameof.hpp>Use the same options in every translation unit to avoid ODR violations.
-
NAMEOF_VERSION_MAJOR,NAMEOF_VERSION_MINORandNAMEOF_VERSION_PATCHhold the library version. -
NAMEOF,NAMEOF_FULL,NAMEOF_RAW, and*_TYPE_EXPRdo not evaluate their arguments. RTTI APIs followtypeidrules and evaluate polymorphic glvalues to obtain the dynamic type.
nameof requires C++17 or newer and supports:
- Clang/LLVM >= 9
- GCC >= 9
- Visual Studio >= 2019
NAMEOF_TYPE_RTTI, NAMEOF_FULL_TYPE_RTTI, and NAMEOF_SHORT_TYPE_RTTI require RTTI to be enabled, so they are unavailable with -fno-rtti or /GR-. NAMEOF_MEMBER and NAMEOF_POINTER require C++20 on Visual Studio before 2022 17.14.
-
Obtains name of variable, function, macro.
-
Returns
nameof::cstring. Markedconstexprandnoexcept. -
Fails to compile for some expressions without a name:
"NAMEOF requires a named expression."See the limitations. -
Examples
// Name of variable. NAMEOF(somevar) -> "somevar" // Name of member variable. NAMEOF(person.address.zip_code) -> "zip_code" // Name of function. NAMEOF(foo<int, float>()) -> "foo" // Name of member function. NAMEOF(somevar.some_method()) -> "some_method" NAMEOF(somevar.some_method<int>()) -> "some_method" // Name of macro. NAMEOF(__LINE__) -> "__LINE__"
-
Obtains name of variable, function, or macro, including template arguments.
-
Returns
nameof::cstring. Markedconstexprandnoexcept. -
Fails to compile for some expressions without a name:
"NAMEOF_FULL requires a named expression."See the limitations. -
Examples
// Full name of template function. NAMEOF_FULL(foo<int, float>()) -> "foo<int, float>" // Full name of template member function. NAMEOF_FULL(somevar.some_method<int>()) -> "some_method<int>"
-
Obtains expression text by preprocessor stringization. Whitespace outside literals is normalized.
-
Returns
nameof::cstring. Markedconstexprandnoexcept. -
Examples
NAMEOF_RAW(::somevar.somefield) -> "::somevar.somefield" NAMEOF_RAW(&some_class::some_method<int>) -> "&some_class::some_method<int>"
-
Obtains name of enum value.
-
Returns
string_view. Markedconstexprandnoexcept. -
Returns a custom or reflected name, otherwise an empty
string_view. -
Automatic lookup with compiler-specific reflection is limited to the enum range. A non-empty range requires an enumerator or a custom name, otherwise compilation fails. Use
NAMEOF_ENUM_ORfor fallback lookup. -
Examples
auto color = Color::RED; NAMEOF_ENUM(color) -> "RED" nameof::nameof_enum(color) -> "RED"
-
Obtains name of enum value or a fallback.
-
Returns a
stringwith the custom or reflected enum name, ordefault_value. -
Examples
auto color = Color::RED; NAMEOF_ENUM_OR(color, "none") -> "RED" NAMEOF_ENUM_OR((Color)-1, "none") -> "none" nameof::nameof_enum_or(color, "none") -> "RED" nameof::nameof_enum_or((Color)-1, "none") -> "none"
-
Obtains name of enum value known at compile time.
-
Returns
nameof::name_view. Markedconstexprandnoexcept. -
Has a lower compile-time cost than
NAMEOF_ENUMand ignoresenum_range. -
Returns a custom or reflected name, otherwise an empty
nameof::name_view. -
Examples
NAMEOF_ENUM_CONST(Color::GREEN) -> "GREEN" nameof::nameof_enum<Color::GREEN>() -> "GREEN"
-
Obtains name of enum flag value.
-
Returns
string. -
Joins custom or reflected names of individual set bits with
sep('|'by default). Composite enumerator names are ignored. -
Zero gives its name, such as
"None", or an emptystringwithNAMEOF_NO_NAMED_ZERO_IN_FLAGS. A value with an unnamed bit gives an emptystring. -
Compiler-specific reflection requires a single-bit enumerator, a named zero or a custom flag name, otherwise compilation fails.
-
Examples
enum AnimalFlags { HasClaws = 1 << 0, CanFly = 1 << 1, EatsFish = 1 << 2, Endangered = 1 << 3 }; auto flag = Endangered; NAMEOF_ENUM_FLAG(flag) -> "Endangered" nameof::nameof_enum_flag(flag) -> "Endangered" flag = static_cast<AnimalFlags>(CanFly | Endangered); NAMEOF_ENUM_FLAG(flag) -> "CanFly|Endangered" nameof::nameof_enum_flag(flag) -> "CanFly|Endangered" nameof::nameof_enum_flag(flag, '$') -> "CanFly$Endangered" NAMEOF_ENUM(static_cast<AnimalFlags>(HasClaws | CanFly)) -> "" nameof::nameof_enum(static_cast<AnimalFlags>(HasClaws | CanFly)) -> ""
Exact type spellings in the examples are compiler-specific. See the type limitations.
-
Obtains type name. Reference and cv-qualifiers are ignored (
NAMEOF_TYPE(const T&) == NAMEOF_TYPE(T)). -
Returns
nameof::name_view. Markedconstexprandnoexcept. -
Examples
using T = const int&; NAMEOF_TYPE(T) -> "int" nameof::nameof_type<T>() -> "int"
-
Obtains full type name with reference and cv-qualifiers.
-
Returns
nameof::name_view. Markedconstexprandnoexcept. -
Examples
using T = const int&; NAMEOF_FULL_TYPE(T) -> "const int&" nameof::nameof_full_type<T>() -> "const int&"
-
Obtains short type name.
-
Returns
nameof::name_view. Markedconstexprandnoexcept. -
If type has no short name, compilation fails:
"nameof::nameof_short_type requires a type with a short name." -
Examples
using T = const my::detail::SomeClass<int>&; NAMEOF_SHORT_TYPE(T) -> "SomeClass" nameof::nameof_short_type<T>() -> "SomeClass"
-
Obtains type name of expression. Reference and cv-qualifiers are ignored.
-
Returns
nameof::name_view. Markedconstexprandnoexcept. -
Examples
using T = const int&; T var = 42; NAMEOF_TYPE_EXPR(var) -> "int" nameof::nameof_type<decltype(var)>() -> "int"
-
Obtains the name of
decltype(expr), preserving reference and cv-qualifiers. -
Returns
nameof::name_view. Markedconstexprandnoexcept. -
Examples
using T = const int&; T var = 42; NAMEOF_FULL_TYPE_EXPR(var) -> "const int&" nameof::nameof_full_type<decltype(var)>() -> "const int&" int x = 0; NAMEOF_FULL_TYPE_EXPR(x) -> "int" NAMEOF_FULL_TYPE_EXPR((x)) -> "int&"
-
Obtains short type name of expression.
-
Returns
nameof::name_view. Markedconstexprandnoexcept. -
If type has no short name, compilation fails:
"nameof::nameof_short_type requires a type with a short name." -
Examples
const my::detail::SomeClass<int> var; NAMEOF_SHORT_TYPE_EXPR(var) -> "SomeClass" nameof::nameof_short_type<decltype(var)>() -> "SomeClass"
-
Obtains type name using RTTI. Reference and cv-qualifiers are ignored.
-
Takes an expression, not a type.
-
Returns
string. -
Examples
my::detail::Derived derived; const volatile my::detail::Base* ptr = &derived; NAMEOF_TYPE_RTTI(*ptr) -> "my::detail::Derived"
-
Obtains type name using RTTI and preserves reference and cv-qualifiers.
-
Takes an expression, not a type.
-
Pointer, array, function, and member pointer types use compile-time names.
-
Returns
string. -
Examples
my::detail::Derived derived; const volatile my::detail::Base& cv_ref = derived; NAMEOF_FULL_TYPE_RTTI(cv_ref) -> "const volatile my::detail::Derived&"
-
Obtains short type name using RTTI. Namespace qualification, template arguments, references, and cv-qualifiers are omitted.
-
Takes an expression, not a type.
-
Returns
string. -
Examples
my::detail::Derived derived; const volatile my::detail::Base* ptr = &derived; NAMEOF_SHORT_TYPE_RTTI(*ptr) -> "Derived"
-
Obtains name of member.
-
Returns
nameof::name_view. Markedconstexprandnoexcept. -
If member pointer is null, returns
"nullptr"by default. -
Examples
struct A { int this_is_the_name; }; // ... NAMEOF_MEMBER(&A::this_is_the_name) -> "this_is_the_name" nameof::nameof_member<&A::this_is_the_name>() -> "this_is_the_name"
-
Obtains name of a function, global variable, or static data member.
-
Returns
nameof::name_view. Markedconstexprandnoexcept. -
If pointer is null, returns
"nullptr"by default. -
Examples
int someglobalvariable = 0; const int someglobalconstvariable = 42; // ... NAMEOF_POINTER(&someglobalconstvariable) -> "someglobalconstvariable" nameof::nameof_pointer<&someglobalconstvariable>() -> "someglobalconstvariable" constexpr auto global_ptr = &someglobalvariable; NAMEOF_POINTER(global_ptr) -> "someglobalvariable" nameof::nameof_pointer<global_ptr>() -> "someglobalvariable"