Giter Club home page Giter Club logo

nameof's Introduction

A header only C++ library provides nameof() macro to obtain the std::string name of a variable, type, or member.

It works like nameof() in C#.

nameof() accepts expression which should be compilable. If expression is compilable but not correspond expressions which shown below, the std::logic_error exception will be thrown.

Usage

First you should download and include nameof.h file.

    #include "nameof.h"

You can get the name of a variable.

    int bar;
    std::string name = nameof(bar); // "bar"

You can get the name of a function. You should use an ampersand (&).

    void foo();
    std::string name = nameof(&foo); // "foo"

You can get the name of an enum element.

    enum Foo {Bar};
    std::string name = nameof(Foo::Bar); // "Bar"

You can get the name of a class or a struct.

    class Foo {};
    std::string name = nameof(Foo); // "Foo"

You can get the name of a method. Do not forget to add an ampersand (&).

    struct Foo { int bar(); };
    std::string name = nameof(&Foo::bar); // "bar"

You can get the name of a class or struct field.

    struct Foo { int bar; };
    std::string name = nameof(Foo::bar); // "bar"

You can get the name of a field of an object.

    struct Foo { int bar; };
    Foo foo;
    std::string name = nameof(foo.bar); // "bar"

You can use ::, -> and . operators with any depth of nesting of entities and with namespaces.

    struct Foo1
    {
        struct Foo2
        {
            Foo1* foo1;
        };

        Foo1* foo1;
        Foo2 foo2;
    };
    std::string name = nameof(Foo1::foo1->foo2.foo1); // "foo1"

Testing

Dependencies

  • Boost

Start

    cd test
    make

Good luck!

nameof's People

Contributors

bravikov avatar pjpjq avatar wmcnamara avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.