检测参数presence与否在C宏 [英] Detect presence or absence of arguments in a C macro

查看:128
本文介绍了检测参数presence与否在C宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人怎么可以定义一个C宏 IFARGS(YES,NO,...)这样调用 IFARGS 没有额外的参数生成 NO ,并调用 IFARGS 与一个或多个参数产生

How can one define a C macro IFARGS(YES, NO, ...) such that invoking IFARGS with no additional arguments produces NO, and invoking IFARGS with one or more arguments produces YES?

我在使用GCC(见下文)的答案,但我preFER一为C99,如果可能的(或不可能性的证明)。

I have an answer using GCC (see below), but I'd prefer one for C99 if possible (or a proof of its impossibility).

推荐答案

在C99就可以检测一个宏参数是空的,但要做出这样强大的对抗可能出现在参数中的所有可能性(被自己不断扩大的论点,包含()键,这样的东西)是困难的。我的宏包 P99 实现了这样的事情,这样你就不必过于担心。与您的宏可以实现为

In C99 it is possible to detect if a macro argument is empty, but making that robust against all odds that may appear in that argument (arguments that are themselves expanding, contain () and stuff like that) is difficult. My macro package P99 implements such a thing, so you wouldn't have to worry too much. With that your macro can be implemented as

#define IFARGS(YES, NO, ...) P99_IF_EMPTY(__VA_ARGS__)(YES(__VA__ARGS__))(NO())

正如其名称所示,P99仅仅建立在C99的功能为。

As its name indicates, P99 is only building on C99 features for that.

这篇关于检测参数presence与否在C宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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