Objective:
I want my class method to retrieve the caller function signature. In the same way __PRETTY_FUNCTION__
does for a local function.
Below you encounter a sample code and the output. I want the output 2 to be like 3.
Restriction: MyClass::foo can't be static. Otherwise, it would be enough to create a macro like
#define foo() MyClass::foo(__PRETTY_FUNCTION__)
after the class declaration and we would have our solution.
#include <iostream>
class MyClass {
public:
void foo(const char* parent = __builtin_FUNCTION()) {
std::cout << "1: " << __PRETTY_FUNCTION__ << std::endl;
std::cout << "2: " << parent << std::endl;
}
};
namespace Space
{
void bar(int notUsedParam){
MyClass myClass;
myClass.foo();
std::cout << "3: " << __PRETTY_FUNCTION__ << std::endl;
}
}
int main() {
Space::bar(1);
return 0;
}
Output:
1: void MyClass::foo(const char*)
2: bar
3: void Space::bar(int)
Some Reference: https://clang.llvm.org/docs/LanguageExtensions.html#source-location-builtins