zoukankan      html  css  js  c++  java
  • C++11 新特性一增加了 __func__宏

    在C11的新特性中,新增加了宏定义 __func__ 用来描述直接得到当函数的名称。

    如:

    const char* hello() {return __func__;} //返回hello.

    也可作为初始化参数传递如:

    struct TestStruct {
     TestStruct (): name(__func__){}  //返回TestStruct结构体类型。
     const char* name; 
    };

    __VAR_ARGS__ 表示可变参数的宏串,如下

    #define LOG(...) {
         fprintf(stderr, "%s: Line %d: ", __FILE__, __LINE__);
         fprintf(stderr, __VA_ARGS__);
         fprintf(stderr, " ");
        }

    这样就可以用一个宏实际了,标准输出的行为了。

     1 //demo.cpp 
     2 
     3 #include <iostream>
     4 
     5 using namespace std;
     6 
     7 const char* hello() {return __func__;}
     8 const char* world() {return __func__;}
     9 //====================1========================
    10 struct TestStruct {
    11     TestStruct (): name(__func__){}
    12     const char* name;    
    13 };
    14 //====================2========================
    15 #define LOG(...) {
    16                     fprintf(stderr, "%s: Line %d:	", __FILE__, __LINE__);
    17                     fprintf(stderr, __VA_ARGS__);
    18                     fprintf(stderr, "
    ");
    19                 }
    20 //====================3========================
    21 int main(int argc, char **args){
    22     cout << "Standard Clib: " << __STDC_HOSTED__ << endl;
    23     cout << "Standard C: "    << __STDC__ << endl;
    24     
    25     cout << "ISO/IEC " << __STDC_ISO_10646__ << endl;
    26     cout << hello() << ", " << world() << endl;
    27     //====================1========================
    28     TestStruct ts;
    29     cout << ts.name << endl;
    30     //====================2========================
    31     
    32     int x = 3;
    33     LOG("x = %d", x);
    34     
    35     //====================3========================
    36     
    37     
    38     
    39     
    40     return 0;
    41 }
    42 
    43 // gcc -std=c++11 demo.cpp -omain
  • 相关阅读:
    mysql general log使用介绍
    是否可以根据GTID 选出日志最新的实例
    python踩坑现场,看起来一样的两个字符串,却不相等
    sql case when的使用
    golang 匿名结构体成员,具名结构体成员,继承,组合
    golang go-sql-driver/mysql基本原理
    raft协议中的日志安全性
    go get 安装 go.etcd.io etcd clientv3 报错
    ZGC
    发现jdk9之后,AQS代码有啥变化了吗
  • 原文地址:https://www.cnblogs.com/liuxw7/p/6533369.html
Copyright © 2011-2022 走看看