I was wondering if it is possible in C++ to retrieve the name of a class in string form without having to hardcode it into a variable or a getter. I\'m aware that none of th
With C++17, and a third-party library, you can now obtain the name of a class like
#include
#include "nameof.hpp"
namespace test {
class Object {};
}
int main() {
constexpr auto obj_name = nameof::nameof_type();
std::cout << obj_name << std::endl;
// this prints "test::Object"
}
This uses only compile-time information, so it can be constexpr. Note that it’s not portable; for example Intel’s compiler isn’t supported.