#include <iostream>
template<typename T>
std::string getClassName()
{
std::string fullFuncName = __PRETTY_FUNCTION__;
const char start[] = "with T = ";
const char end[] = ";";
auto beginning = fullFuncName.find(start);
auto ending = fullFuncName.find(end, beginning);
return {fullFuncName.begin() + beginning + sizeof(start) - 1,
fullFuncName.begin() + ending};
}
struct Test
{
Test()
{
std::cout << "Created instance of " << getClassName<std::decay_t<decltype(*this)>>() << '\n';
}
};
template<typename Derived>
struct IPrintable
{
static std::string getMyName()
{
return getClassName<Derived>();
}
};
struct TestPrintable : IPrintable<TestPrintable>
{
void sayHello() const
{
std::cout << "Hi, I'm " << getMyName() << '\n';
}
};
int main() {
std::cout << getClassName<int>() << '\n';
Test test;
TestPrintable testPrintable;
testPrintable.sayHello();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdGVtcGxhdGU8dHlwZW5hbWUgVD4Kc3RkOjpzdHJpbmcgZ2V0Q2xhc3NOYW1lKCkKewoJc3RkOjpzdHJpbmcgZnVsbEZ1bmNOYW1lID0gX19QUkVUVFlfRlVOQ1RJT05fXzsKCWNvbnN0IGNoYXIgc3RhcnRbXSA9ICJ3aXRoIFQgPSAiOwoJY29uc3QgY2hhciBlbmRbXSA9ICI7IjsKCWF1dG8gYmVnaW5uaW5nID0gZnVsbEZ1bmNOYW1lLmZpbmQoc3RhcnQpOwoJYXV0byBlbmRpbmcgPSBmdWxsRnVuY05hbWUuZmluZChlbmQsIGJlZ2lubmluZyk7CglyZXR1cm4ge2Z1bGxGdW5jTmFtZS5iZWdpbigpICsgYmVnaW5uaW5nICsgc2l6ZW9mKHN0YXJ0KSAtIDEsCgkJCWZ1bGxGdW5jTmFtZS5iZWdpbigpICsgZW5kaW5nfTsKfQoKc3RydWN0IFRlc3QKewoJVGVzdCgpCgl7CgkJc3RkOjpjb3V0IDw8ICJDcmVhdGVkIGluc3RhbmNlIG9mICIgPDwgZ2V0Q2xhc3NOYW1lPHN0ZDo6ZGVjYXlfdDxkZWNsdHlwZSgqdGhp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