在C / C ++中,__FUNCTION__,__func__、__PRETTY_FUNCTION__和__FUNCSIG__的作用及区别

在这里,我们将看到C ++中__FUNCTION __,__ func__和__PRETTY_FUNCTION__之间的区别是什么。

__FUNCTION__和__func__基本上是相同的。某些旧版本的C和C ++支持__func__。该宏用于获取当前函数的名称。_PRETTY_FUNCTION__用于返回有关该函数的详细信息。使用此函数,我们可以获取使用哪个函数,以及该函数属于哪个类,等等。

#include<iostream>
using namespace std;
class MyClass{
   public:
      void Class_Function(){
         cout << "The result of __PRETTY_FUNCTION__: " << __PRETTY_FUNCTION__ << endl;
      }
};
void TestFunction(){
   cout << "Output of __func__ is: " << __func__ << endl;
}
main() {
   cout << "Output of __FUNCTION__ is: " << __FUNCTION__ << endl;
   TestFunction();
   MyClass myObj;
   myObj.Class_Function();
}

输出结果

Output of __FUNCTION__ is: main
Output of __func__ is: TestFunction
The result of __PRETTY_FUNCTION__: void MyClass::Class_Function()

参考:

https://www.nhooo.com/note/qa0shb.html

二、在 C/C++ 语言中特定的宏,如 __FUNCTION__

__FILE__

__LINE__

__DATE__

__TIME__

__FUNC__

__func__

__FUNCTION__

在Visual Studio 2005中,默认情况下,此特性是激活的,但不能与/EP和/P编译选项同时使用。请注意在IDE环境中,不能识别__func__ ,而要用__FUNCTION__ 代替。

Comeau的用户也应使用 __FUNCTION__ ,而不是 __func__ 。

C++ BuilderX的用户则应使用稍稍不同的名字:__FUNC__ 。

GCC 3.0及更高的版本同时支持 __func__ 和__FUNCTION__ 。

==============================================================

仅仅为了获取函数名,就在函数体中嵌入硬编码的字符串,这种方法单调乏味还易导致错误,不如看一下怎样使用新的C99特性,在程序运行时获取函数名吧。

  对象反射库、调试工具及代码分析器,经常会需要在运行时访问函数的名称,直到不久前,唯一能完成此项任务并且可移植的方法,是手工在函数体内嵌入一个带有该函数名的硬编码字符串,不必说,这种方法非常单调无奇,并且容易导致错误。本文将要演示怎样使用新的C99特性,在运行时获取函数名。

  那么怎样以编程的方式从当前运行的函数中得到函数名呢?

  答案是:使用__FUNCTION__ 及相关宏。

  引出问题

  通常,在调试中最让人心烦的阶段,是不断地检查是否已调用了特定的函数。对此问题的解决方法,一般是添加一个cout或printf()——如果你使用C语言,如下所示:

void myfunc()
{
    cout<<"myfunc()"<<endl;
    //其他代码
}

通常在一个典型的工程中,会包含有数千个函数,要在每个函数中都加入一条这样的输出语句,无疑难过上“蜀山”啊,因此,需要有一种机制,可以自动地完成这项操作。

  获取函数名

  作为一个C++程序员,可能经常遇到 __TIME__、__FILE__、__DATE__ 这样的宏,它们会在编译时,分别转换为包含编译时间、处理的转换单元名称及当前时间的字符串。

  在最新的ISO C标准中,如大家所知的C99,加入了另一个有用的、类似宏的表达式__func__,其会报告未修饰过的(也就是未裁剪过的)、正在被访问的函数名。请注意,__func__不是一个宏,因为预处理器对此函数一无所知;相反,它是作为一个隐式声明的常量字符数组实现的:

static const char __func__[] = "function-name";

在function-name处,为实际的函数名。为激活此特性,某些编译器需要使用特定的编译标志,请查看相应的编译器文档,以获取具体的资料。

  有了它,我们可免去大多数通过手工修改,来显示函数名的苦差事,以上的例子可如下所示进行重写:

void myfunc()
{
    cout<<"__FUNCTION__"<<endl;
}

官方C99标准为此目的定义的__func__标识符,确实值得大家关注,然而,ISO C++却不完全支持所有的C99扩展,因此,大多数的编译器提供商都使用 __FUNCTION__ 取而代之,而 __FUNCTION__ 通常是一个定义为 __func__ 的宏,之所以使用这个名字,是因为它已受到了大多数的广泛支持。

  在Visual Studio 2005中,默认情况下,此特性是激活的,但不能与/EP和/P编译选项同时使用。请注意在IDE环境中,不能识别__func__ ,而要用__FUNCTION__ 代替。

  Comeau的用户也应使用 __FUNCTION__ ,而不是 __func__ 。

  C++ BuilderX的用户则应使用稍稍不同的名字:__FUNC__ 。

  GCC 3.0及更高的版本同时支持 __func__ 和__FUNCTION__ 。

  一旦可自动获取当前函数名,你可以定义一个如下所示显示任何函数名的函数:

void show_name(const char * name)
{
cout<<name<<endl;
}

void myfunc()
{
show_name(__FUNCTION__); //输出:myfunc
}

void foo()
{
show_name(__FUNCTION__); //输出:foo
}

 因为 __FUNCTION__ 会在函数大括号开始之后就立即初始化,所以,foo()及myfunc()函数可在参数列表中安全地使用它,而不用担心重载。

  签名与修饰名

  __FUNCTION__ 特性最初是为C语言设计的,然而,C++程序员也会经常需要有关他们函数的额外信息,在Visual Studio 2005中,还支持另外两种非标准的扩展特性:__FUNCDNAME__ 与 __FUNCSIG__ ,其分别转译为一个函数的修饰名与签名。函数的修饰名非常有用,例如,在你想要检查两个编译器是否共享同样的ABI时,就可派得上用场,另外,它还能帮助你破解那些含义模糊的链接错误,甚至还可用它从一个DLL中调用另一个用C++链接的函数。在下例中,show_name()报告了函数的修饰名:

void myfunc()
{
show_name(__FUNCDNAME__); //输出:?myfunc@@YAXXZ
}

一个函数的签名由函数名、参数列表、返回类型、内含的命名空间组成。如果它是一个成员函数,它的类名和const/volatile限定符也将是签名的一部分。以下的代码演示了一个独立的函数与一个const成员函数签名间的不同之处,两个函数的名称、返回类型、参数完全相同:

void myfunc()
{
show_name(__FUNCSIG__); // void __cdecl myfunc(void)
}

struct S
{
void myfunc() const
{
show_name(__FUNCSIG__); //void __thiscall S::myfunc(void) const
}
};

参考:

https://blog.csdn.net/dafan/article/details/5781491

三、C++__PRETTY_FUNCTION__和__FUNCSIG__宏简单使用

这里给出例子。

CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)

project ( testprj )

set ( PRJ_COMPILE_FEATURES )
set ( PRJ_COMPILE_DEFINITIONS )

message ( STATUS "MSVC      = ${MSVC}" )

if ( MSVC )
    list ( APPEND PRJ_COMPILE_DEFINITIONS USE_MSVC )
endif()

list ( APPEND PRJ_COMPILE_FEATURES cxx_std_20 )

add_executable( ${PROJECT_NAME}
    main.cpp
)

target_compile_features ( ${PROJECT_NAME} 
    PRIVATE 
        ${PRJ_COMPILE_FEATURES}
)

target_compile_definitions ( ${PROJECT_NAME}
    PRIVATE
       ${PRJ_COMPILE_DEFINITIONS} 
)

main.cpp

#include <iostream>

#ifdef USE_MSVC
    #define FUNCTION_DETAIL __FUNCSIG__
#else
    #define FUNCTION_DETAIL __PRETTY_FUNCTION__
#endif

void print()
{
    std::cout << FUNCTION_DETAIL << ": " << std::endl;
}

template<typename T, typename... Args>
void print(T t, Args... args)
{
    std::cout << FUNCTION_DETAIL << ": " << t << std::endl;
    print(args...);
}


int main()
{
    print( 1, 2.5, 'a', 2022, "hello!");
    return 0;
}

windows11+powershell

cmake ..

PS D:\work\modern_cmake_work\ModernCMake\codes\moderncpp\variadic\variadic01\build> cmake ..
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22000.
-- The C compiler identification is MSVC 19.30.30709.0
-- The CXX compiler identification is MSVC 19.30.30709.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.30.30705/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.30.30705/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- MSVC      = 1
-- Configuring done
-- Generating done
-- Build files have been written to: D:/work/modern_cmake_work/ModernCMake/codes/moderncpp/variadic/variadic01/build

cmake --build .

PS D:\work\modern_cmake_work\ModernCMake\codes\moderncpp\variadic\variadic01\build> cmake --build .
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 17.0.0+c9eb9dd64
版权所有(C) Microsoft Corporation。保留所有权利。

  Checking Build System
  Building Custom Rule D:/work/modern_cmake_work/ModernCMake/codes/moderncpp/variadic/variadic01/CMakeLists.txt
  main.cpp
  testprj.vcxproj -> D:\work\modern_cmake_work\ModernCMake\codes\moderncpp\variadic\variadic01\build\Debug\testprj.exe
  Building Custom Rule D:/work/modern_cmake_work/ModernCMake/codes/moderncpp/variadic/variadic01/CMakeLists.txt

.\Debug\testprj.exe

PS D:\work\modern_cmake_work\ModernCMake\codes\moderncpp\variadic\variadic01\build> .\Debug\testprj.exe
void __cdecl print<int,double,char,int,const char*>(int,double,char,int,const char *): 1
void __cdecl print<double,char,int,const char*>(double,char,int,const char *): 2.5
void __cdecl print<char,int,const char*>(char,int,const char *): a
void __cdecl print<int,const char*>(int,const char *): 2022
void __cdecl print<const char*,>(const char *): hello!
void __cdecl print(void):

在ubuntu21.10下:

cmake ..

eric@ubuntu:~/work/cmake_work/study/variadic/variadic01/build$ cmake ..
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- MSVC      = 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/eric/work/cmake_work/study/variadic/variadic01/build

cmake --build .

eric@ubuntu:~/work/cmake_work/study/variadic/variadic01/build$ cmake --build .
Scanning dependencies of target testprj
[ 50%] Building CXX object CMakeFiles/testprj.dir/main.cpp.o
[100%] Linking CXX executable testprj
[100%] Built target testprj

运行:

./testprj

eric@ubuntu:~/work/cmake_work/study/variadic/variadic01/build$ ./testprj 
void print(T, Args ...) [with T = int; Args = {double, char, int, const char*}]: 1
void print(T, Args ...) [with T = double; Args = {char, int, const char*}]: 2.5
void print(T, Args ...) [with T = char; Args = {int, const char*}]: a
void print(T, Args ...) [with T = int; Args = {const char*}]: 2022
void print(T, Args ...) [with T = const char*; Args = {}]: hello!
void print(): 

注意到windows和linux的输出略有略有不同。

参考:

https://zhuanlan.zhihu.com/p/466735374

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

https://en.cppreference.com/w/cpp/preprocessor/replace

https://zhuanlan.zhihu.com/p/465469553

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值