什么是一些技巧,我可以用宏使用? [英] What are some tricks I can use with macros?

查看:116
本文介绍了什么是一些技巧,我可以用宏使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的传统code,以及我们的现代code,我们用宏来实现诸如code世代等漂亮的解决方案,我们利用这两个 ## 运营商。

In our legacy code, as well as our modern code, we use macros to perform nifty solutions like code generations, etc. And we make use of both the # and ## operators.

我很好奇,其他开发人员如何使用宏做很酷的事情,如果他们使用它们。

I am curious how other developers use macros to do cool things, if they use them at all.

推荐答案

在C,这是常见的定义做一些东西得到逐字参数宏,并在同一时间定义功能能够得到它的地址透明。

In C, it's common to define macros that do some stuff getting the verbatim argument, and at the same time define functions to be able to get the address of it transparently.

// could evaluate at compile time if __builtin_sin gets
// special treatment by the compiler
#define sin(x) __builtin_sin(x)

// parentheses avoid substitution by the macro
double (sin)(double arg) {
    return sin(arg); // uses the macro
}

int main() {
    // uses the macro
    printf("%f\n", sin(3.14));

    // uses the function
    double (*x)(double) = &sin;

    // uses the function
    printf("%f\n", (sin)(3.14));
}

这篇关于什么是一些技巧,我可以用宏使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆