Visual C++相当于 __FILE__ 、 __LINE__ 和 __PRETTY_FUNCTION__

发布于 2024-10-07 18:38:42 字数 249 浏览 4 评论 0原文

GCC 编译器为我提供了以下宏:

  • __FILE__,以便我可以打印出文件名+目录。
  • __LINE__ 这样我就可以打印出我正在打印的行号。
  • __PRETTY_FUNCTION__ 这样我就可以打印出漂亮的函数名称

Visual C++ 是否有与这些宏等效的功能?附带问题是,这些是 C++ 编译器的标准吗?

GCC compiler gives me the following macros:

  • __FILE__ so that I can print out the file name + directory.
  • __LINE__ so that I can print out the line number of where I'm printing from.
  • __PRETTY_FUNCTION__ so that I can print out the pretty function name

Does Visual C++ have the equivalent of these macros? A side question is, are these standard for C++ compilers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

窗影残 2024-10-14 18:38:42

在 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);
}

将打印这个:(

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

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

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.)

差↓一点笑了 2024-10-14 18:38:42

__FILE____LINE__ 是标准的,我相当确定 Microsoft 编译器基本上一直都有它们。

__PRETTY_FUNCTION__ 是 gcc 功能。

__FILE__ and __LINE__ are standard, and I'm rather certain Microsoft compilers have essentially always had them.

__PRETTY_FUNCTION__ is a gcc feature.

樱桃奶球 2024-10-14 18:38:42

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

For more portability in getting the current function name, you can try BOOST_CURRENT_FUNCTION.

老旧海报 2024-10-14 18:38:42

是的,Visual C++ 有它们或等效的东西。请参阅此处的回复:

__PRETTY_FUNCTION__、__FUNCTION__、__func__ 之间有什么区别?
function-func/4384860#4384860

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

Yes Visual C++ has them or an equivalent. See the responses here:

What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__?
function-func/4384860#4384860

Also note that despite the upper case used, they aren't macros. They're variables.

昇り龍 2024-10-14 18:38:42

我知道MSVC提供了__FILE____LINE__,它们都是标准宏。他们还提供 __FUNCTION__ ,我相信这就是您正在寻找的,

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,

木緿 2024-10-14 18:38:42

__FILE____LINE__ 是自 C89 以来的标准。 __PRETTY_FUNCTION__ 是一种 GCCism。 __func__ 是一种 C99ism(与 GCCism 不同),很可能在 Visual C++ 中可用;它与 __PRETTY_FUNCTION__ 并不完全相同,但对于您的目的而言,它可能足够接近。

__FILE__ and __LINE__ are standard since C89. __PRETTY_FUNCTION__ is a GCCism. __func__ is a C99ism which (unlike GCCisms) may well be available in Visual C++; it is not precisely the same as __PRETTY_FUNCTION__ but it may be close enough for your purposes.

梦里南柯 2024-10-14 18:38:42
  1. 是的,Microsoft Visual Studio 有 __FILE____LINE__这里有更多 MSVC 宏

  2. 两者都是 ANSI C++。

  3. MSVC 具有 __FUNCTION__,这是 Microsoft 特定的。

  1. Yes, Microsoft Visual Studio has __FILE__ and __LINE__. Here are more MSVC macros.

  2. Both are ANSI C++.

  3. MSVC has __FUNCTION__, which is Microsoft-specific.

羁〃客ぐ 2024-10-14 18:38:42

使用constexpr 你可以使用这个:<代码>WHERE宏。

基于以下内容的使用:

#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;

完整&运行示例

请参阅:

Using 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__)))

Example usage:

    // Called: void (anonymous namespace)::exampleUseCaseWhere(int):18
    std::cout << "Called: " << WHERE << std::endl;

Full & running example here

See:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文