c++用宏定义来编写函数#define

来源:互联网 发布:出现无法安装java 编辑:程序博客网 时间:2024/06/07 07:10
#include<iostream>#include<stdlib.h>#include<assert.h>#include"hello.h"#ifdef _OPENMP#include<omp.h>#endif#define CHECK(arg){                 \                                    \    cout<<"CHECK:"<<arg<<endl;      \}static int _T;#define CHECKOPENMP(arg)    \{                           \    _T = 10;                \}using namespace std;typedef struct Node{    int data;    struct Node *lchild;    struct Node *rchild;}Node, *BinTree;static ssize_tfun(){    cout<<"fun"<<endl;}int main(int argv,char*argc[]){    register int threadnum = 1;    assert(argv-1);    #ifdef _OPENMP    if(argv > 1){        threadnum = atoi(argc[1]);        omp_set_num_threads(threadnum);    }    #pragma omp parallel for reduction(+:_T)    for(int i=0;i<5;i++)        _T += i;    #endif    fun();    CHECK(__FILE__);    CHECK(__LINE__);    cout<<_T<<endl;    CHECKOPENMP();    _CHECK();    cout<<_T<<endl;    #ifdef _hello    cout<<max(12,14)<<endl;    #endif    cout<<"Hello World!"<<endl;        return 1;    }


hello.h如下:

#ifndef _hello#define _helloint max(int a, int b){    return a>b?a:b;}typedef struct cigbsegy{    int a;    int b;    int c;}cigbsegy, SEGY;#endif#define _CHECK(arg)  \{                    \    cout<<"_CHECK"<<endl; \}

结果:

rongtao@rongtao:~/test$ g++ -o a hello.cpp -fopenmprongtao@rongtao:~/test$ ./a 3funCHECK:hello.cppCHECK:5410_CHECK1014Hello World!rongtao@rongtao:~/test$ 


原创粉丝点击