Visual C++ 等价于 __FILE__ 、 __LINE__ 和 __PRETTY_FUNCTION__
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4434282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual C++ equivalent of __FILE__ , __LINE__ and __PRETTY_FUNCTION__
提问by sivabudh
GCC compiler gives me the following macros:
GCC 编译器给了我以下宏:
__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
__FILE__
这样我就可以打印出文件名+目录。__LINE__
这样我就可以打印出我打印位置的行号。__PRETTY_FUNCTION__
这样我就可以打印出漂亮的函数名称
Does Visual C++ have the equivalent of these macros? A side question is, are these standard for C++ compilers?
Visual C++ 是否具有与这些宏等效的功能?附带的问题是,这些是 C++ 编译器的标准吗?
回答by Leo Davidson
In VS2008, this:
在 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);
}
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.)
(行号是“错误的”,因为我的文件顶部确实有一些额外的东西。)
回答by aschepler
__FILE__
and __LINE__
are standard, and I'm rather certain Microsoft compilers have essentially always had them.
__FILE__
并且__LINE__
是标准的,而且我很确定 Microsoft 编译器基本上一直都有它们。
__PRETTY_FUNCTION__
is a gcc feature.
__PRETTY_FUNCTION__
是一个 gcc 功能。
回答by Fred Larson
For more portability in getting the current function name, you can try BOOST_CURRENT_FUNCTION.
为了获得当前函数名称的更多可移植性,您可以尝试BOOST_CURRENT_FUNCTION。
回答by sashang
Yes Visual C++ has them or an equivalent. See the responses here:
是的 Visual C++ 有它们或等效的。请参阅此处的回复:
What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__?function-func/4384860#4384860
__PRETTY_FUNCTION__、__FUNCTION__、__func__ 有什么区别?函数功能/4384860#4384860
Also note that despite the upper case used, they aren't macros. They're variables.
另请注意,尽管使用了大写字母,但它们不是宏。它们是变量。
回答by zwol
__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.
__FILE__
并且__LINE__
是自 C89 以来的标准。 __PRETTY_FUNCTION__
是海湾合作委员会主义。 __func__
是一种 C99ism,它(与 GCCisms 不同)很可能在 Visual C++ 中可用;它并不完全相同,__PRETTY_FUNCTION__
但对于您的目的来说可能足够接近。
回答by Puppy
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,
我知道 MSVC 提供__FILE__
和__LINE__
,两者都是标准宏。他们还提供__FUNCTION__
,我相信这正是您要找的,
回答by EboMike
Yes, Microsoft Visual Studio has
__FILE__
and__LINE__
. Here are more MSVC macros.Both are ANSI C++.
MSVC has
__FUNCTION__
, which is Microsoft-specific.
是的,Microsoft Visual Studio 具有
__FILE__
和__LINE__
. 这里有更多 MSVC 宏。两者都是ANSI C++。
MSVC 有
__FUNCTION__
,这是 Microsoft 特定的。
回答by Dawid Drozd
Using c++14with constexpr
you can use this: WHERE
macro.
使用C ++ 14与constexpr
您可以使用此:WHERE
宏。
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
完整和运行示例在这里