Visual C++相当于__FILE __,_ _ _ ____和__PRETTY_FUNCTION__

siv*_*udh 19 c++ compiler-construction gcc visual-c++

GCC编译器给我以下宏:

  • __FILE__ 这样我就可以打印出文件名+目录了.
  • __LINE__ 这样我就可以打印出我要打印的行号.
  • __PRETTY_FUNCTION__ 这样我就可以打印出漂亮的功能名称了

Visual C++是否具有这些宏的等价物?一个侧面问题是,这些是C++编译器的标准吗?

Leo*_*son 31

在VS2008中,这个:

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);
}
Run Code Online (Sandbox Code Playgroud)

将打印此:

c:\source\test_projects\blah\blah.cpp
14
A::Test
?Test@A@@QAE_NH@Z
bool __thiscall A::Test(int)
Run Code Online (Sandbox Code Playgroud)

(行号是"错误的",因为我的文件顶部确实有一些额外的东西.)


asc*_*ler 9

__FILE__并且__LINE__是标准的,我相信微软编译器基本上总是拥有它们.

__PRETTY_FUNCTION__ 是一个gcc功能.


Fre*_*son 7

为了获得当前函数名称的更多可移植性,您可以尝试BOOST_CURRENT_FUNCTION.


sas*_*ang 6

是Visual C++有它们或等价物.请参阅此处的回复:

__PRETTY_FUNCTION __,__ FUNCTION __,__ func__之间有什么区别? 功能FUNC/4384860#4384860

另请注意,尽管使用了大写字母,但它们不是宏.他们是变数.