it is not common when you see a lot of C language has something similar to the following defintion.
#define macro_name(arg) do { \
// ... \
} while (0);
but why do we have this touble, why to guard against some innocent piece of code with the nugatory/inconsequential code as above?
let's see some example. first I defined several macros.
/**
* file: macros.cpp
* description: demonstrate how to use macros, and some of the techniques.
*/
#define hash(x) do { \
int y = 0; \
if ((x) > 0) y = ((x) << 1) + 1; \
else y = ((-x) << 1) + 1; \
y; \
} while (0);
#define hash2(x) {\
int y = 0; \
if ((x) > 0) y = ((x) << 1) + 1; \
else y = ((-x) << 1) + 1; \
y; \
}
#define hash3(x) \
int y = 0; \
if ((x) > 0) y = ((x) << 1) + 1; \
else y = ((-x) << 1) + 1; \
y;
and the code to test it.
// so apparantly you cannot use the macro in some if/while condition statement.
//if (hash(1) != 3) {
// cout << "hash macros works" << endl;
//}
// but this is ok , you can treat the hash(i) as a single line of function.
for (int i = 0; i < 10; i ++) hash(i);
// this is also OK, because there is a local scope indicator, but that should only work in C++, because C does not have local scope inside a local function?
for (int i = 0; i < 10; i ++) hash2(i);
// this is NOT ok, when the code is expanded, you will see errors
//for (int i = 0; i < 10; i++) hash3(i);
as you cna see, that with the necessary do while statement, and with the help from compiler (because it can optimize the hopeless do while (0); and run the code within only once), you can treat the macros defined as a single line code - many expect it to be.
分享到:
相关推荐
赠送jar包:spire-macros_2.12-0.13.0.jar; 赠送原API文档:spire-macros_2.12-0.13.0-javadoc.jar; 赠送源代码:spire-macros_2.12-0.13.0-sources.jar; 赠送Maven依赖信息文件:spire-macros_2.12-0.13.0.pom;...
util-macros-1.11.0.tar.bz2 qca9531
官方离线安装包,亲测可用
官方离线安装包,亲测可用
buildroot交叉编译x11vnc时需要用到的util-macros工具源码
官方离线安装包,亲测可用
spire-macros_2.11-0.7.4.jar
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
util-macros-1.19.2.tar.bz2
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,亲测可用
赠送jar包:breeze-macros_2.12-0.13.2.jar; 赠送原API文档:breeze-macros_2.12-0.13.2-javadoc.jar; 赠送源代码:breeze-macros_2.12-0.13.2-sources.jar; 包含翻译后的API文档:breeze-macros_2.12-0.13.2-...
赠送jar包:breeze-macros_2.12-0.13.2.jar; 赠送原API文档:breeze-macros_2.12-0.13.2-javadoc.jar; 赠送源代码:breeze-macros_2.12-0.13.2-sources.jar; 赠送Maven依赖信息文件:breeze-macros_2.12-0.13.2....
赠送jar包:spire-macros_2.12-0.13.0.jar; 赠送原API文档:spire-macros_2.12-0.13.0-javadoc.jar; 赠送源代码:spire-macros_2.12-0.13.0-sources.jar; 赠送Maven依赖信息文件:spire-macros_2.12-0.13.0.pom;...
在Laravel框架中,"Macros"是一种非常实用的功能,它允许我们自定义全局助手函数或者扩展现有的类,以提高代码的可复用性和可读性。Macros主要用于Blade模板、Eloquent ORM(对象关系映射)以及其他核心类库。下面...
在Laravel框架的开发环境中,Laravel IDE Macros是一个非常实用的工具,它旨在提升开发者在IDE(集成开发环境)中的编码体验。这个工具允许我们创建自定义的代码片段,也就是所谓的“宏”,这些宏可以自动补全代码,...
离线安装包,亲测可用
离线安装包,亲测可用
django-macros-url, Django 宏的URL路由必须尽可能简单 Django MACROS URL v0.4.0路由必须尽可能简单Django MACROS URL使得使用 MACROS 可以以轻松地在 Django 应用程序中编写( 阅读) URL模式。例如可以将前缀与宏名...