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.
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"
- Boost
cd test
make
Good luck!