效果展示
//a scoped enumeration
enum enumeration{ MONO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN};
int main(void){
char buf[1024U] {};
//borrow a char-block buffer
//tell the 'name' of 5 in an enum
indicator<enumeration, static_cast<enumeration>(5U)>(buf);
std::cout << buf << '\n';
return 0;
}
实现如下
#include <iostream>
#include <cstring>
#include <string>
template <typename __ENUM__, __ENUM__ __TPYE__>
constexpr char const* indicator(char const* buf){
const_cast<char *>(buf)[0] = '\0';
auto eq { std::strchr(__PRETTY_FUNCTION__, '=') + 2U};
auto _eq { std::strchr(eq, '=') + 2U };
auto sem { std::strchr(eq, ';') };
auto pr { std::strchr(__PRETTY_FUNCTION__, ']') };
auto len { sem - eq };
auto _len { pr - _eq };
std::strncpy(const_cast<char *>(buf), eq, len);
std::strcat(const_cast<char *>(buf), "::");
std::strncat(const_cast<char *>(buf), _eq, _len);
return buf;
}