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.
// Simple Example.
#include <iostream>
#include <cstdint>
#include <nameof.hpp>
@ -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
}
}

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.
#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>
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>(#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; }
// 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); }
#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)