C++ 宏/关键字哪个可用于打印出方法名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2192680/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:28:22  来源:igfitidea点击:

Macro / keyword which can be used to print out method name?

c++c

提问by Notinlist

__FILE__and __LINE__are well known. There is a __func__since C99.

__FILE__并且__LINE__是众所周知的。有一个__func__自C99。

#include <iostream>
struct Foo {
        void Do(){ std::cout << __func__ << std::endl; }
};

int main()
{
        std::cout << __func__ << std::endl;
        Foo foo; foo.Do();
        return 0;
}

will output

会输出

main
Do

Is there any macro / keyword that would output method name like Foo::Do?

是否有任何宏/关键字可以输出方法名称,例如Foo::Do

回答by Kornel Kisielewicz

Boost has a special utility macro called BOOST_CURRENT_FUNCTIONthat hides the differences between the compiler implementations.

Boost 有一个名为BOOST_CURRENT_FUNCTION的特殊实用程序宏,它隐藏了编译器实现之间的差异。

Following it's implementation we see that there are several macros depending on compiler:

在它的实现之后,我们看到有几个取决于编译器的宏:

  • __PRETTY_FUNCTION__-- GCC, MetroWerks, Digital Mars, ICC, MinGW
  • __FUNCSIG__-- MSVC
  • __FUNCTION__-- Intel and IBM
  • __FUNC__-- Borland
  • __func__-- ANSI C99
  • __PRETTY_FUNCTION__-- GCC、MetroWerks、数字火星、ICC、MinGW
  • __FUNCSIG__-- MSVC
  • __FUNCTION__——英特尔和IBM
  • __FUNC__-- 博兰德
  • __func__-- ANSI C99

回答by rpg

  • On GCC you can use __FUNCTION__and __PRETTY_FUNCTION__.
  • On MSVC you can use __FUNCSIG__and __FUNCTION__.
  • 在 GCC 上,您可以使用__FUNCTION____PRETTY_FUNCTION__
  • 在 MSVC 上,您可以使用__FUNCSIG____FUNCTION__

回答by David Thornley

There's no such macro in standard C++, and that includes the draft C++0x standard I looked at. It would complicate compilation, since parsing (necessary to determine what a function is) comes after preprocessing, and I suspect that's why there's nothing in the standard.

标准 C++ 中没有这样的宏,其中包括我查看的 C++0x 标准草案。这会使编译复杂化,因为解析(确定函数是什么所必需的)是在预处理之后进行的,我怀疑这就是标准中没有任何内容的原因。

The __func__you're using is nonstandard, although it apparently works on your compiler.

__func__你使用的是非标准,但它显然适用于你的编译器。

回答by David Thornley

Not in Standard C++ (and __func__is not part of C++). Your implementation may have such a feature though - which compiler are you using?

不在标准 C++ 中(并且__func__不是 C++ 的一部分)。不过,您的实现可能具有这样的功能 - 您使用的是哪个编译器?

回答by Ruddy

See "Predefined Macros (C/C++)" for a complete list supported by MS Visual Studio.

有关MS Visual Studio 支持的完整列表,请参阅“预定义宏 (C/C++)”。

回答by Mayank

This might be useful:

这可能有用:

http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html

http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html

#include"stdio.h"
#include"stdlib.h"
int main()
{
printf("line number is %d .. func name is %s, file name is %s",__LINE__,__PRETTY_FUNCTION__,__FILE__);
return  0;
}

This is how to print line number, function name and file name in gcc.

这是如何在 gcc 中打印行号、函数名和文件名。