前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C99显示函数名的宏__func__

C99显示函数名的宏__func__

作者头像
mythsman
发布2022-11-14 16:17:58
3160
发布2022-11-14 16:17:58
举报

前言

在调试代码或者写一些通用的测试函数的时候,有时候想如果能够用字符串的方式显示出正在调用的函数名该有多好。其实在C99标准中就已经有了这样的宏__func__,只是平常不怎么被提起。那么这个宏怎么用呢?

示例

程序:

#include<iostream>
using namespace std;
void fun(){
    cout<<__func__<<endl;
}
int main(){
    fun();
}

结果:

fun

没错,就是这么简单,非常方便。特别在某些调用函数指针进行测试的函数里,用这个东西来显示当前所调用的不同函数还是特别轻松的。比如:

#include<iostream>
using namespace std;
string insertionSort(int *a , int n){
    //------
    return string(__func__);
}
string bubbleSort(int *a , int n){
    //------
    return string(__func__);
}
string quickSort(int *a , int n){
    //------
    return string(__func__);
}
string mergeSort(int *a , int n){
    //------
    return string(__func__);
}

void test(void (*sort)(int *,int),int *a,int n){
    int time;
    //-----
    cout<<(*sort)(a,n)<<time<<endl;
}
int main(){
    int a[100];
    //----
    test(insertionSort,a,100);
    test(bubbleSort,a,100);
    test(quickSort,a,100);
    test(mergeSort,a,100);
}

这就可以非常轻松的显示各个方法调用后运行的结果了,而不用手动写函数名了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档