Corrected the names macros

This commit is contained in:
terik23 2018-03-17 09:36:39 +05:00
parent 9adf5c882f
commit 853a895e6c
2 changed files with 15 additions and 12 deletions

View file

@ -22,6 +22,7 @@
// SOFTWARE. // SOFTWARE.
// Simple Example. // Simple Example.
#include <iostream> #include <iostream>
#include <cstdint> #include <cstdint>
#include <nameof.hpp> #include <nameof.hpp>
@ -36,13 +37,13 @@ int someVar = 0;
int main() { int main() {
SomeStruct someVar{1}; SomeStruct someVar{1};
constexpr auto a = nameof_variable(someVar.SomeField); constexpr auto a = NAMEOF_VAR(someVar.SomeField);
constexpr auto b = nameof_variable((&someVar)->SomeField); constexpr auto b = NAMEOF_VAR((&someVar)->SomeField);
constexpr auto c = nameof_variable(someVar); constexpr auto c = NAMEOF_VAR(someVar);
constexpr auto d = nameof_variable(::someVar); constexpr auto d = NAMEOF_VAR(::someVar);
constexpr auto e = nameof_variable(&SomeStruct::SomeMethod); constexpr auto e = NAMEOF_VAR(&SomeStruct::SomeMethod);
constexpr auto f = nameof_function(someVar.SomeMethod()); constexpr auto f = NAMEOF_FUN(someVar.SomeMethod());
constexpr auto g = nameof_type(SomeStruct); constexpr auto g = NAMEOF_TYPE(SomeStruct);
std::cout << a << std::endl; // SomeField std::cout << a << std::endl; // SomeField
std::cout << b << std::endl; // SomeField std::cout << b << std::endl; // SomeField

View file

@ -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. // 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 <typename T> template <typename T>
inline constexpr const char* template_nameof_variable(const T& validate_type, const char* name, size_t length) { return template_nameof_(name, length); } 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. // Used to obtain the string name of a type.
#define nameof_type(type) template_nameof_type<type>(#type) #define NAMEOF_TYPE(type) template_nameof_type<type>(#type)
template <typename T> inline constexpr const char* template_nameof_type(const char* name) { return name; } template <typename T> inline constexpr const char* template_nameof_type(const char* name) { return name; }
// Used to obtain the string name of a function. // 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); #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); } 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)