C/C++中#define的妙用分享

本文主要介绍了C++/C关于#define的一些妙用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.数值类型输出易读的字符串形式

例如使用enum定义一些错误值,想要将数值类型的错误,输出易读的字符串形式

重要的一句代码

#define MAKE_PAIR(val) std::make_pair(val, #val) 

可以看到 #val,宏定义中的传入参数名val 转换成字符串,就像用一对双引号包含起来的val

完整实现代码如下

#include  #include  #include  #include  #include  #include  using namespace std; typedef enum { ACAMERA_OK = 0, ACAMERA_ERROR_BASE                  = -10000, ACAMERA_ERROR_UNKNOWN               = ACAMERA_ERROR_BASE, ACAMERA_ERROR_INVALID_PARAMETER     = ACAMERA_ERROR_BASE - 1, ACAMERA_ERROR_CAMERA_DISCONNECTED   = ACAMERA_ERROR_BASE - 2, } camera_status_t; #define UKNOWN_TAG "UNKNOW_TAG" #define MAKE_PAIR(val) std::make_pair(val, #val) template  const char* GetPairStr(T key, std::vector>& store) { typedef typename std::vector>::iterator iterator; for (iterator it = store.begin(); it != store.end(); ++it) { if (it->first == key) { return it->second; } } //LOGW("(%#08x) : UNKNOWN_TAG for %s", key, typeid(store[0].first).name()); return UKNOWN_TAG; } using ERROR_PAIR = std::pair; static std::vector errorInfo{ MAKE_PAIR(ACAMERA_OK), MAKE_PAIR(ACAMERA_ERROR_UNKNOWN), MAKE_PAIR(ACAMERA_ERROR_INVALID_PARAMETER), MAKE_PAIR(ACAMERA_ERROR_CAMERA_DISCONNECTED), }; const char* GetErrorStr(camera_status_t err) { return GetPairStr(err, errorInfo); } int main() { std::cout<

输出

ACAMERA_ERROR_INVALID_PARAMETER

2.易记的简化调用

例如有两个函数

camera_status_t ACameraManager_A() { std::cout<<"A"<

这两个函数很长,函数名前缀相同

想要易记的简化调用

例如

CALL_MGR(A()); //实际调用ACameraManager_A() CALL_MGR(B()); //实际调用ACameraManager_B() 
#define CALL_CAMERA(func)                                             \ {                                                                   \ camera_status_t status = func;                                    \ std::cout<

#define 后面的 \ 表示下一行继续写宏定义。

两个#号 ## 表示连接操作符。 CALL_MGR(A());通过 ACameraManager_##func 变成了ACameraManager_A

实现完整代码如下

#include  #include  #include  #include  #include  #include  #include  using namespace std; typedef enum { ACAMERA_OK = 0, ACAMERA_ERROR_BASE                  = -10000, ACAMERA_ERROR_UNKNOWN               = ACAMERA_ERROR_BASE, ACAMERA_ERROR_INVALID_PARAMETER     = ACAMERA_ERROR_BASE - 1, ACAMERA_ERROR_CAMERA_DISCONNECTED   = ACAMERA_ERROR_BASE - 2, } camera_status_t; #define UKNOWN_TAG "UNKNOW_TAG" #define MAKE_PAIR(val) std::make_pair(val, #val) template  const char* GetPairStr(T key, std::vector>& store) { typedef typename std::vector>::iterator iterator; for (iterator it = store.begin(); it != store.end(); ++it) { if (it->first == key) { return it->second; } } //LOGW("(%#08x) : UNKNOWN_TAG for %s", key, typeid(store[0].first).name()); return UKNOWN_TAG; } using ERROR_PAIR = std::pair; static std::vector errorInfo{ MAKE_PAIR(ACAMERA_OK), MAKE_PAIR(ACAMERA_ERROR_UNKNOWN), MAKE_PAIR(ACAMERA_ERROR_INVALID_PARAMETER), MAKE_PAIR(ACAMERA_ERROR_CAMERA_DISCONNECTED), }; const char* GetErrorStr(camera_status_t err) { return GetPairStr(err, errorInfo); } camera_status_t ACameraManager_A() { std::cout<<"A"<

输出

A
ACAMERA_OK
B
ACAMERA_OK

以上代码应用在google的ndk camera代码中

到此这篇关于C/C++中#define的妙用分享的文章就介绍到这了,更多相关C++ #define内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是C/C++中#define的妙用分享的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言