GCC compiler gives me the following macros:
__FILE__
so that I can print out the file name + directory.__LINE__
so that I c
Using c++14 with constexpr you can use this: WHERE
macro.
Based on usage of:
#include "string/ConstexprString.hpp"
#define S1(x) #x
#define S2(x) S1(x)
// WHERE - const char* const should be used as temporary value
#define WHERE (string::make(__PRETTY_FUNCTION__) + ":" + string::make(S2(__LINE__))).get()
// It is safe to store e.g. `constexpr auto where = WHERE_STR;`
#define WHERE_STR (string::make(__PRETTY_FUNCTION__) + ":" + string::make(S2(__LINE__)))
// Called: void (anonymous namespace)::exampleUseCaseWhere(int):18
std::cout << "Called: " << WHERE << std::endl;
Full & running example here
__FILE__
and __LINE__
are standard, and I'm rather certain Microsoft compilers have essentially always had them.
__PRETTY_FUNCTION__
is a gcc feature.
In VS2008, this:
struct A
{
bool Test(int iDummyArg)
{
const char *szFile = __FILE__;
int iLine = __LINE__;
const char *szFunc = __FUNCTION__; // Func name
const char *szFunD = __FUNCDNAME__; // Decorated
const char *szFunS = __FUNCSIG__; // Signature
printf("%s\n", szFile);
printf("%d\n", iLine);
printf("%s\n", szFunc);
printf("%s\n", szFunD);
printf("%s\n", szFunS);
return true;
}
};
int wmain(int argc, TCHAR *lpszArgv[])
{
A a;
a.Test(10);
}
will print this:
c:\source\test_projects\blah\blah.cpp
14
A::Test
?Test@A@@QAE_NH@Z
bool __thiscall A::Test(int)
(The line number is "wrong" since there was really some extra stuff at the top of my file.)
I know that MSVC offers __FILE__
and __LINE__
, both of which are Standard macros. They also offer __FUNCTION__
, which I believe is what you're looking for,
For more portability in getting the current function name, you can try BOOST_CURRENT_FUNCTION.
Yes, Microsoft Visual Studio has __FILE__
and __LINE__
. Here are more MSVC macros.
Both are ANSI C++.
MSVC has __FUNCTION__
, which is Microsoft-specific.