nameof_module/nameof.cpp

15 lines
771 B
C++
Raw Normal View History

2016-10-05 09:14:37 +00:00
///Used to obtain the string name of a variable.
2016-10-09 15:27:12 +00:00
#define nameof_variable(name) template_nameof_variable(name, #name)
2016-10-09 15:35:11 +00:00
template <typename T> const char* template_nameof_variable(const T& validate_type, const char* name) { return name; }
2016-10-05 09:14:37 +00:00
2016-10-09 15:27:12 +00:00
///Used to obtain the string name of a type.
#define nameof_type(name) template_nameof_type<name>(#name)
2016-10-09 15:35:11 +00:00
template <typename T> const char* template_nameof_type(const char* name) { return name; }
2016-10-05 09:14:37 +00:00
2016-10-09 15:27:12 +00:00
//example
int test = 0;
std::cout << nameof_variable(test) << " " << nameof_type(int) << std::endl;
// prints "test int”
std::string test_wrong;
std::cout << nameof_variable(static_cast<std::string>(test_wrong.c_str())) << " " << nameof_type(std::string) << std::endl;
// prints "static_cast<std::string>(test_wrong.c_str()) std::string"