Reputation: 13597
I want to get full function path and declaration in code via macro or some compiler magic. I have this code (click here to run):
#include <iostream>
namespace NS {
struct Foo {
static int sum(int a, int b) {
std::cout << "This is from " << __FILE__ << ":" << __LINE__ << " @ " << __func__ << std::endl;
return a+b;
}
static int sum(int a, int b, int c) {
std::cout << "This is from " << __FILE__ << ":" << __LINE__ << " @ " << __func__ << std::endl;
return a+b+c;
}
};
}
int main() {
NS::Foo::sum(1,2);
NS::Foo::sum(1,2, 3);
}
And I get the output:
This is from /some/where/main.cpp:7 @ sum
This is from /some/where/main.cpp:12 @ sum
My questions are:
sum
function invoked?(NS::Foo::sum)
sum(int, int)
or sum(int, int, int)
)I am interested in mainstream compilers: Clang, GCC, Microsoft C++ compiler
Upvotes: 0
Views: 426
Reputation: 238361
If you don't want to limit yourself to a single compiler, and don't fancy writing a pre-processor ifdef chain yourself, boost has already defined a macro BOOST_CURRENT_FUNCTION
which is defined as the full function signature macro that's defined on the used compiler. __PRETTY_FUNCTION__
in gcc and other compilers that support that, __FUNCSIG__
where that is supported (msvc), also supports some lesser used compilers and falls back to __func__
as defined by the C standard (or a static placeholder if even that's not supported).
Upvotes: 2
Reputation: 5209
For Microsoft Visual Studio compiler, __FUNCSIG__
can give you a lot about the function.
__FUNCSIG__
will give you full function signature
__FUNCDNAME__
gives magnled name.
__FUNCTION__
is just for function name.
Upvotes: 3
Reputation: 5279
Answering for GCC.
Checkout the __PRETTY_FUNCTION__
macro. I've found it in the defintion of assert
macro in <assert.h>
. Perhaps same could be found for other compilers and libc implementations.
Upvotes: 3