Nameof operator for modern C++, simply obtain the name of a variable, type, function, macro, and enum

Overview
 _   _                             __    _____
| \ | |                           / _|  / ____|_     _
|  \| | __ _ _ __ ___   ___  ___ | |_  | |   _| |_ _| |_
| . ` |/ _` | '_ ` _ \ / _ \/ _ \|  _| | |  |_   _|_   _|
| |\  | (_| | | | | | |  __/ (_) | |   | |____|_|   |_|
|_| \_|\__,_|_| |_| |_|\___|\___/|_|    \_____|

Github releases Conan package Vcpkg package License Build status Build status Codacy badge Try online Compiler explorer

Nameof C++

Header-only C++17 library provides nameof macros and functions to simply obtain the name of a variable, type, function, macro, and enum.

Features

  • C++17
  • Header-only
  • Dependency-free
  • Compile-time
  • Name of variable, member variable
  • Name of type, variable type
  • Name of function, member function
  • Name of enum, enum variable
  • Name of macro
  • Enum to string

Documentation

Examples

  • Nameof

    // 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__"
    NAMEOF(NAMEOF(structvar)) -> "NAMEOF"
  • Nameof enum

    enum class Color { RED = 1, BLUE = 2, GREEN = 4 };
    
    auto color = Color::RED;
    // Name of enum variable.
    NAMEOF_ENUM(color) -> "RED"
    nameof::nameof_enum(color) -> "RED"
    
    // Static storage enum variable to string.
    // This version is much lighter on the compile times and is not restricted to the enum_range limitation.
    NAMEOF_ENUM_CONST(Color::GREEN) -> "GREEN"
    nameof::nameof_enum<Color::GREEN>() -> "GREEN"
    
    // Enum flag variable to string.
    NAMEOF_ENUM_FLAG(Color::GREEN | Color::BLUE) -> "GREEN|BLUE"
    nameof::nameof_enum_flag<Color::GREEN>() -> "GREEN|BLUE"
  • Nameof type

    const my::detail::SomeClass<int>& var_ref = var;
    // Name of variable type.
    NAMEOF_TYPE_EXPR(var_ref) -> "my::detail::SomeClass<int>"
    nameof::nameof_type<decltype(var_ref)>() -> "my::detail::SomeClass<int>"
    NAMEOF_FULL_TYPE_EXPR(var_ref) -> "const my::detail::SomeClass<int>&"
    nameof::nameof_full_type<decltype(var_ref)>() -> "const my::detail::SomeClass<int>&"
    NAMEOF_SHORT_TYPE_EXPR(var_ref) -> "SomeClass"
    nameof::nameof_short_type<decltype(var_ref)>() -> "SomeClass"
    
    using T = const my::detail::SomeClass<int>&;
    // Name of type.
    NAMEOF_TYPE(T) ->"my::detail::SomeClass<int>"
    nameof::nameof_type<T>() -> "my::detail::SomeClass<int>"
    NAMEOF_FULL_TYPE(T) -> "const my::detail::SomeClass<int>&"
    nameof::nameof_full_type<T>() -> "const my::detail::SomeClass<int>&"
    NAMEOF_SHORT_TYPE(T) -> "SomeClass"
    nameof::nameof_short_type<T>() -> "SomeClass"
    
    my::detail::Base* ptr = new my::detail::Derived();
    // Name of type, using rtti.
    NAMEOF_TYPE_RTTI(*ptr) -> "my::detail::Derived"
    NAMEOF_FULL_TYPE_RTTI(*ptr) -> "volatile const my::detail::Derived&"
    NAMEOF_SHORT_TYPE_RTTI(*ptr) -> "Derived"
  • Compile-time

    constexpr auto somevar_name = NAMEOF(somevar);
    // somevar_name -> "somevar"
    constexpr auto color_name = NAMEOF_ENUM(Color::BLUE); // or nameof::nameof_enum(Color::BLUE)
    // color_name -> "BLUE"
    constexpr auto var_type_name = NAMEOF_TYPE_EXPR(var); // or nameof::nameof_type<decltype(var)>()
    // var_type_name -> "int"
    constexpr auto type_name = NAMEOF_TYPE(T); // or nameof::nameof_type<T>()
    // type_name -> "int"

Remarks

Integration

You should add required file nameof.hpp.

If you are using vcpkg on your project for external dependencies, then you can use the nameof package.

If you are using Conan to manage your dependencies, merely add nameof/x.y.z to your conan's requires, where x.y.z is the release version you want to use.

Alternatively, you can use something like CPM which is based on CMake's Fetch_Content module.

CPMAddPackage(
    NAME nameof
    GITHUB_REPOSITORY Neargye/nameof
    GIT_TAG x.y.z # Where `x.y.z` is the release version you want to use.
)

Compiler compatibility

  • Clang/LLVM >= 5
  • MSVC++ >= 14.11 / Visual Studio >= 2017
  • Xcode >= 10
  • GCC >= 7 (GCC >= 9 for NAMEOF_ENUM)

Licensed under the MIT License

Issues
  • Migrate to conan ci

    Migrate to conan ci

    This PR does the following things:

    • uses conan to acquire Catch2 if and only if the nameof lib is build using the build_test option
    • uses Appveyor and travis to make sure that the nameof library is usable on certain configuration and all the tests pass and the example builds
    • it will upload the package (recipe, nameof.hpp and the generated nameofConfig,cmake ... files) to bintray with the version specified in the conanfile.py.

    Note that

    The CI services can run any time you push a commit, it won't upload any versions to bintray, only if you create a new tag (with any name pattern) like v0.9.1 It won't upload any built binaries (neither tests, nor example), but it will upload the sources of the tests and example, since a consumer may want to build it himself.

    enhancement 
    opened by steakhal 16
  • MSVC Warning C26495 Variable nameof::cstring<14>::chars is uninitialized

    MSVC Warning C26495 Variable nameof::cstring<14>::chars is uninitialized

    Our nightly static code analysis build with VS 2017 15.9.4 reported this warning:

    Severity	Code	Description	Line	File	Project	Suppression State
    Warning	C26495	Variable 'nameof::cstring<14>::chars' is uninitialized. Always initialize a member variable (type.6).	92	c:\users\hutchinsons\source\dart\ng-dart\source\vs2012\ng-dart base\nameof.hpp	NG-DART	Active
    

    I attempted to fix it by changing const std::array<char, N + 1> chars; to const std::array<char, N + 1> chars{ {} };, but that did not help.

    Any idea how to fix this? Thanks

    help wanted 
    opened by ScottHutchinson 10
  • MSVC puts 'class ' prefix, but gcc doesn't

    MSVC puts 'class ' prefix, but gcc doesn't

    For the following template class:

    template <typename T>
    class SomeClass {};
    

    msvc and gcc returns different results for the following expression:

    nameof::nameof_type<SomeClass<int>>()
    

    msvc returns class SomeClass<int> unlike gcc which returns just SomeClass<int>.

    Should it be in this way?

    question 
    opened by steakhal 10
  • Some questions related to your CMakeLists.txts

    Some questions related to your CMakeLists.txts

    There are several questions to the structure of your CMakeLists.txt.

    1. Why branch on this? CMakeLists.txt:7-11 link there
    if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
        set(IS_TOPLEVEL_PROJECT TRUE)
    else()
        set(IS_TOPLEVEL_PROJECT FALSE)
    endif()
    
    1. Why do you try to specify the standard by hand? example/CMakeLists.txt link there You could use generator expressions which are well described in this blogpost. This would be more readable I think.
    include(CheckCXXCompilerFlag)
    
    if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
        check_cxx_compiler_flag(-std=c++17 HAS_CPP17_FLAG)
        if(!HAS_CPP17_FLAG)
            MESSAGE(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support.")
        endif()
    
        set(CMAKE_VERBOSE_MAKEFILE ON)
        set(OPTIONS -Wall -Wextra -pedantic-errors -Werror -std=c++17)
    elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
        check_cxx_compiler_flag(/std:c++17 HAS_CPP17_FLAG)
    # more ...
    
    1. At the same CMakeLists.txt you should depend on the nameof target, instead of just simply including the nameof header. link there
    add_executable(example
                        example.cpp
                        ${CMAKE_SOURCE_DIR}/include/${CMAKE_PROJECT_NAME}.hpp)
    
    1. Is it a bad practice to bake in the projects name? I think that would be more readable than using the ${CMAKE_PROJECT_NAME}. (It's only a question.)

    2. What is going on in test/CMakeLists.txt I don't really get it. Like creating two test binaries, but why? I think it should be simplified. It also suffers from the problem which I described at the second bullet-point. link there

    if(HAS_CPP17_FLAG)
        make_test(${CMAKE_PROJECT_NAME}-cpp17.t c++17)
    endif()
    
    if(HAS_CPPLATEST_FLAG)
        make_test(${CMAKE_PROJECT_NAME}-cpplatest.t c++latest)
    endif()
    
    question 
    opened by steakhal 9
  • request: operator std::string

    request: operator std::string

    I see that there's an operator std::string_view; could you add an operator std::string? Functions that take a std::string require an explicit cast to pass in.

    Example:

    void foo(const std::string &asdf) {}
    void bar()
    {
    	const char *foo1;
    	foo(NAMEOF(foo1));  // error: no suitable user-defined conversion from "nameof::cstring<4UL>" to "const std::string" exists
    	foo(NAMEOF(foo1).data()); // ok
    	foo(std::string(NAMEOF(foo1))); // ok
    }
    

    This is compiling using MSVC's CL version 19.00.24234.1

    question wontfix 
    opened by arolson101 8
  • Created conan.io recipe for this package.

    Created conan.io recipe for this package.

    I did not find your email address, so I write to you here.

    I created a Conan recipe for your package. You can find it here: https://github.com/steakhal/conan-nameof

    If you are no familiar with Conan, you can find more about it here: https://conan.io/ tldr. it is a package manager for c++

    We can merge the recipe into your repository if you want to.

    enhancement 
    opened by steakhal 8
  • Error C2338: static_assert failed: 'nameof::nameof_enum requires enum implementation and valid max and min.'

    Error C2338: static_assert failed: 'nameof::nameof_enum requires enum implementation and valid max and min.'

    Hi guys. Today Micorsoft release VS2022 17.2, then the nameof is not happy with "Error C2338: static_assert failed: 'nameof::nameof_enum requires enum implementation and valid max and min.'" The code is in 897 line: static_assert(detail::count_v<D> > 0, "nameof::nameof_enum requires enum implementation and valid max and min.");

    invalid 
    opened by icegull 7
  • x86 msvc v19.27 static code analysis warning C28020 after updating nameof.hpp to v0.10.0

    x86 msvc v19.27 static code analysis warning C28020 after updating nameof.hpp to v0.10.0

    Will try to find the code that triggers this when I have time. ...\nameof.hpp (579, 0) ...\nameof.hpp(579,0): Warning C28020: The expression '0<=Param(1)&&Param(1)<=1-1' is not true at this call.

    opened by ScottHutchinson 5
  • NAMEOF_ENUM compiled failed with MSVC 17.2.2

    NAMEOF_ENUM compiled failed with MSVC 17.2.2

    Thank you for this excellent program that has helped me so much in my work!

    I currently have a problem that after upgrading the MSVC version to 17.2.2, the previous code does not compile properly.

    packages are configured via vcpkg and all up to date.

    as follows.(compiled with -std=c++20)

    #include <nameof.hpp>
    #include <fmt/format.h>
    
    enum class ObjectiveLensMedia {
        Air = 0,
        Oil = 1
    };
    
    int main() {
        fmt::print("{}.\n", NAMEOF_ENUM(ObjectiveLensMedia::Air));
    }
    

    Will give me a static assert failed with message 'nameof::nameof_enum requires enum implementation and valid max and min.'.

    I'm very confused about this and would like help, a lot of my code depends on this.

    Thank you very much!

    duplicate 
    opened by YunchengLiu 3
  • nameof.hpp(274): warning C26495: Variable 'nameof::detail::static_string<19>::chars' is uninitialized. Always initialize a member variable (type.6).

    nameof.hpp(274): warning C26495: Variable 'nameof::detail::static_string<19>::chars' is uninitialized. Always initialize a member variable (type.6).

    Similar to Issue #15 previously resolved.

    This MSVC static code analysis warning is triggered by code like this:

    auto enu = static_cast<SOME_ENUM_t>(nSomeEnumeratorValue);
    
    if (!nameof::nameof_enum(enu).empty()) { ... }
    
    opened by ScottHutchinson 2
  • This library is on Vcpkg

    This library is on Vcpkg

    This library is on Vcpkg

    As it turned out somebody already uploaded this library to Vcpkg's repository. We should keep this in mind if we tag a new release, to issue a PR to Vcpkg's repository where we update the refs of the latest release.

    https://github.com/microsoft/vcpkg/blob/master/ports/nameof/CONTROL

    Maybe integrate into the CI loop? Or do this manually? - I'm not familiar with Vcpkg. Maybe add some reference/badge to the README.md which would advertise the Vcpkg as well besides Conan.

    opened by steakhal 2
  • add MSVC member pointer name in c++20

    add MSVC member pointer name in c++20

    Until C++20, MSVC doesn't supply the member name from member pointer in __FUNCSIG__.

    https://godbolt.org/z/nbGMMjrTf

    With this technique, from the member pointer can be extract the member name.

    enhancement 
    opened by schaumb 2
  • type name without struct/class/enum

    type name without struct/class/enum

    struct A{};
    class B{};
    enum C{};
    template<typename T>
    class D{};
    constexpr auto name_A = nameof::nameof_type<A>(); // "struct A"
    constexpr auto name_B = nameof::nameof_type<B>(); // "class B"
    constexpr auto name_C = nameof::nameof_type<C>(); // "enum C"
    constexpr auto name_D_A = nameof::nameof_type<D<A>>(); // "class D<struct A>"
    

    but I want

    • name_A == "A"
    • name_B == "B"
    • name_C == "C"
    • name_D_A == D<A>

    Is it possible to get them in compile-time?

    enhancement help wanted 
    opened by Ubpa 12
Releases(v0.10.1)
  • v0.10.1(Jun 21, 2021)

  • v0.10.0(Jan 12, 2021)

    Release v0.10.0

    • Add way to switch std::string and std::string_view to custom type
    • Add syntax for defining custom enum and type names
    • Add NAMEOF_ENUM_FLAG
    • Add NAMEOF_SHORT_TYPE
    • Add NAMEOF_TYPE_RTTI, NAMEOF_FULL_TYPE_RTTI, NAMEOF_SHORT_TYPE_RTTI
    • Add NAMEOF_OPT_INSTALL option in CMakeLists.txt
    • Add detect values out of range: checks for a value at (range_min - 1) and (range_max + 1) and fails compilation with a static_assert if any value is found.
    • Fix hangs Intellisense
    • Fix compiler check
    • Fix build error in gcc
    • Fix build msvc with clang
    • Fix build error in clang
    • [breaking changes] enum_range moved to namespace magic_enum::customize
    Source code(tar.gz)
    Source code(zip)
    nameof.hpp(37.31 KB)
  • v0.9.4(Jun 6, 2020)

  • v0.9.3(Dec 30, 2019)

  • v0.9.2(Oct 19, 2019)

    Release v0.9.2

    • Add documentation.

    • nameof and nameof_type returns nameof::cstring - constexpr implementation of an string.

    • Fix nameof_enum cvref regression.

    • Fix nameof_enum bug signed casts to unsigned in min()/max().

    • Fix nameof_enum calculate reflected range.

    • Improving nameof_enum compile times.

    Source code(tar.gz)
    Source code(zip)
    nameof.hpp(23.85 KB)
  • v0.9.1(Oct 2, 2019)

    Release v0.9.1

    • Less bin size and overhead reduction, thanks @rollbear

    • nameof and nameof_type return static_string convertible to std::string_view or std::string.

    • Add error msg unsupported compiler.

    • Marco NAMEOF_ENUM_SUPPORTED and NAMEOF_TYPE_SUPPORTED to check is compiler compatibility.

    • Fix type cast for unsigned enum.

    Source code(tar.gz)
    Source code(zip)
    nameof.hpp(23.61 KB)
  • v0.9.0(Jul 25, 2019)

    Release v0.9.0

    • Fix build fail with sanitize, see https://github.com/Neargye/magic_enum/issues/6

    • Fix implicit conversion changes signedness.

    • Improving compile times.

    • [breaking changes] nameof_type return name same as typeid. NAMEOF_TYPE - obtains string name of type, reference and cv-qualifiers are ignored.

    • [breaking changes] Rename NAMEOF_VAR_TYPE to NAMEOF_TYPE_EXPR. NAMEOF_TYPE_EXPR - obtains string name type of expression, reference and cv-qualifiers are ignored.

    • Add nameof_full_type. NAMEOF_FULL_TYPE - obtains string name of full type, with reference and cv-qualifiers. NAMEOF_FULL_TYPE_EXPR - obtains string name full type of expression, with reference and cv-qualifiers.

    Source code(tar.gz)
    Source code(zip)
    nameof.hpp(11.85 KB)
  • v0.8.3(May 6, 2019)

Owner
Daniil Goncharov
Daniil Goncharov
C++ compile-time enum to string, iteration, in a single header file

Better Enums Reflective compile-time enum library with clean syntax, in a single header file, and without dependencies. In C++11, everything can be us

Anton Bachin 1.3k Jun 23, 2022
Header-only, non-intrusive and macro-free runtime reflection system in C++

Header-only runtime reflection system in C++ The reflection system was born within EnTT and is developed and enriched there. This project is designed

Michele Caini 418 Jun 21, 2022
std::tuple like methods for user defined types without any macro or boilerplate code

Boost.PFR This is a C++14 library for very basic reflection that gives you access to structure elements by index and provides other std::tuple like me

Antony Polukhin 117 Jun 26, 2022
A modern compile-time reflection library for C++ with support for overloads, templates, attributes and proxies

refl-cpp v0.12.1 Documentation refl-cpp encodes type metadata in the type system to allow compile-time reflection via constexpr and template metaprogr

Veselin Karaganev 658 Jun 23, 2022
C++ reflection library with Lua binding, and JSON and XML serialisation.

Ponder Linux & OSX: - Windows: Currents status: 3.2-alpha. API is unstable as features added/changed. New: Version 3 V1 replaced Boost with C++11. V2

Bill Quith 545 Jun 20, 2022
Nameof operator for modern C++, simply obtain the name of a variable, type, function, macro, and enum

_ _ __ _____ | \ | | / _| / ____|_ _ | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |

Daniil Goncharov 1.4k Jun 27, 2022
For C++, help generate a default operator for classes whose components (base classes, data members) have the operator. Hideously intrusive.

C-plus-plus-library-default-operators Helps generate a default operator / member function for classes whose components (base classes, data members) ha

Walt Karas 2 Jul 25, 2018
✔️The smallest header-only GUI library(4 KLOC) for all platforms

Welcome to GUI-lite The smallest header-only GUI library (4 KLOC) for all platforms. 中文 Lightweight ✂️ Small: 4,000+ lines of C++ code, zero dependenc

null 6.3k Jun 27, 2022
Gesture-Detecting-Macro-Keyboard - Glorified Bluetooth macro keyboard with machine learning (TensorFlow Lite for Microcontrollers) running on an ESP32.

Gesture detection tldr; Glorified Bluetooth macro keyboard with machine learning (TensorFlow Lite for Microcontrollers) running on an ESP32. Main feat

Jakob Krantz 63 Jun 19, 2022
Obtain and parse SSL certificates

CheckCert A small utility to request the SSL certificate from a public or private web application. CheckCert helps operators in the following ways: It

Sanjiv Kawa 78 Jun 24, 2022
A local DNS server to obtain the fastest website IP for the best Internet experience

A local DNS server to obtain the fastest website IP for the best Internet experience

Nick Peng 5k Jun 26, 2022
Easy to use, header only, macro generated, generic and type-safe Data Structures in C

C Macro Collections Easy to use, header only, macro generated, generic and type-safe Data Structures in C. Table of Contents Installation Contributing

Leonardo Vencovsky 307 Jun 28, 2022
Easy to use, header only, macro generated, generic and type-safe Data Structures in C

C Macro Collections Easy to use, header only, macro generated, generic and type-safe Data Structures in C. Table of Contents Installation Contributing

Leonardo Vencovsky 307 Jun 28, 2022
A C++14 macro to get the type of the current class without naming it

self_macro C++14 header only library that exposes a macro that creates a type alias for the current class without naming it. Also exposes macros that

Mital Ashok 8 Jan 14, 2022
Quick check of NT kernel exported&unexported functions/global variable offset

NT内核导出以及未导出函数-全局变量偏移速查 Quick check of NT kernel exported&unexported functions/global variable offset System目录下有已经完成的偏移 可以在线速查 There are already comple

不想加班劉 65 Jun 12, 2022
A proof of concept demonstrating instrumentation callbacks on Windows 10 21h1 with a TLS variable to ensure all syscalls are caught.

Instrumentation callbacks are quite a fun undocumented part of Windows. All the code in this repository is released under the MIT license. This repository uses google style C++.

Deputation 38 Jun 22, 2022
Get the ability to use variable argument lists in C++ without requiring the first parameter! Meant to support a WG14 proposal to fix up not having empty argument lists.

Vargs Alright, it's time to commit code crimes for the greater good! What if you did not need to pass an initial parameter to your C++ ... functions?

Shepherd's Oasis 3 Dec 2, 2021
C++ application development framework, to help developers create and deploy applications quickly and simply

ULib - C++ library Travis CI: Coverity Scan: ULib is a highly optimized class framework for writing C++ applications. I wrote this framework as my too

stefano casazza 949 Jun 6, 2022
Flutter package that lets you simply animate a widget into a visible state.

Entry This Flutter package introduces a new widget : Entry. It lets you simply animate a widget into a visible state. Don't just display widgets : mak

Mickaël Hernandez 22 Apr 13, 2022
A simply GUI to change settings in coreboot's CBFS, via the nvramtool utility.

coreboot-configurator A simple GUI to change settings in coreboot's CBFS, via the nvramtool utility. How to install Ubuntu, Linux Mint, elementary OS,

Star Labs 25 Jun 4, 2022
An injector is simply a program that injects some sort of file into your game

example-injector What it injector? An injector is simply a program that injects some sort of file into your game. This could be something as benign as

Speedy 17 Jun 30, 2022
Tiny library for C++ enum introspection and more!

#smart_enum Tiny library for C++ enum introspection... and more! (Sorry for this readme being incomplete, I'm working on updating it. For now, please

Jarda 32 Jan 19, 2022
C++ compile-time enum to string, iteration, in a single header file

Better Enums Reflective compile-time enum library with clean syntax, in a single header file, and without dependencies. In C++11, everything can be us

Anton Bachin 1.3k Jun 23, 2022
A reflective enum implementation for C++

wise_enum Because reflection makes you wise, not smart wise_enum is a standalone smart enum library for C++11/14/17. It supports all of the standard f

Nir Friedman 258 Jun 27, 2022
Improved Fractions Calculator using structures, user input parsing, and dynamic operator switching

Improved Fractions Calculator Program Structure: Main File: Runs fnctions from various header files. IO.h Header file containing IO functions, Interfa

Colin McCormack 1 Dec 5, 2021