diff --git a/example/example.cpp b/example/example.cpp index 736baf8..1ae8d8d 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -22,6 +22,7 @@ // SOFTWARE. // Simple Example. + #include #include #include @@ -36,13 +37,13 @@ int someVar = 0; int main() { SomeStruct someVar{1}; - constexpr auto a = nameof_variable(someVar.SomeField); - constexpr auto b = nameof_variable((&someVar)->SomeField); - constexpr auto c = nameof_variable(someVar); - constexpr auto d = nameof_variable(::someVar); - constexpr auto e = nameof_variable(&SomeStruct::SomeMethod); - constexpr auto f = nameof_function(someVar.SomeMethod()); - constexpr auto g = nameof_type(SomeStruct); + constexpr auto a = NAMEOF_VAR(someVar.SomeField); + constexpr auto b = NAMEOF_VAR((&someVar)->SomeField); + constexpr auto c = NAMEOF_VAR(someVar); + constexpr auto d = NAMEOF_VAR(::someVar); + constexpr auto e = NAMEOF_VAR(&SomeStruct::SomeMethod); + constexpr auto f = NAMEOF_FUN(someVar.SomeMethod()); + constexpr auto g = NAMEOF_TYPE(SomeStruct); std::cout << a << std::endl; // SomeField std::cout << b << std::endl; // SomeField @@ -51,4 +52,4 @@ int main() { std::cout << e << std::endl; // SomeMethod std::cout << f << std::endl; // SomeMethod() std::cout << g << std::endl; // SomeStruct -} \ No newline at end of file +} diff --git a/src/nameof.hpp b/src/nameof.hpp index 2b4681a..69a3df6 100644 --- a/src/nameof.hpp +++ b/src/nameof.hpp @@ -31,14 +31,16 @@ inline constexpr const char* template_nameof_(const char* name, size_t length) { } // Used to obtain the string name of a variable. -#define nameof_variable(variable) template_nameof_variable(variable, #variable, sizeof(#variable) / sizeof(char) - 1) +#define NAMEOF_VARIABLE(variable) template_nameof_variable(variable, #variable, sizeof(#variable) / sizeof(char) - 1) template inline constexpr const char* template_nameof_variable(const T& validate_type, const char* name, size_t length) { return template_nameof_(name, length); } +#define NAMEOF_VAR(var) NAMEOF_VARIABLE(var) // Used to obtain the string name of a type. -#define nameof_type(type) template_nameof_type(#type) +#define NAMEOF_TYPE(type) template_nameof_type(#type) template inline constexpr const char* template_nameof_type(const char* name) { return name; } // Used to obtain the string name of a function. -#define nameof_function(function) template_nameof_function(#function, sizeof(#function) / sizeof(char) - 1); if (false) (void)(function); -inline constexpr const char* template_nameof_function(const char* name, size_t length) { return template_nameof_(name, length); } \ No newline at end of file +#define NAMEOF_FUNCTION(function) template_nameof_function(#function, sizeof(#function) / sizeof(char) - 1); if (false) (void)(function); +inline constexpr const char* template_nameof_function(const char* name, size_t length) { return template_nameof_(name, length); } +#define NAMEOF_FUN(fun) NAMEOF_FUNCTION(fun)