这是什么的#define语法是什么意思? [英] What does this #define syntax mean?

查看:490
本文介绍了这是什么的#define语法是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到这一块,不明白。

 的#define IDEBUG(一...)

什么的(一...)是什么意思?


解决方案

这是一个可变参数宏

从链接页面引用逐字:


  

一个宏可以声明为接受可变数量的参数就像一个功能即可。用于限定宏语法类似于一个函数。下面是一个例子:

 的#define EPRINTF(...)fprintf中(标准错误,__VA_ARGS__)

这类型的宏被称为可变参数。当调用宏,在其参数列表中的所有令牌的最后一个命名参数后(这个宏有没有),包括任何逗号,变成可变参数。令牌的这种顺序替换宏体无论它出现的标识符 VA_ARGS 。因此,我们有这种扩张:

  EPRINTF(%S:%D:,INPUT_FILE,LINENO)
      ==> fprintf中(标准错误,%S:%D:,INPUT_FILE,LINENO)


和针对具体形式,引用页面进一步下跌:


  

如果您的宏很复杂,你可能要超过 __ __ VA_ARGS 变量参数更具描述性的名称。 CPP允许此,作为扩展。 该名称用于可变参数,你可以立即`...'前写的参数名。该EPRINTF宏观上面可以写

 的#define EPRINTF(参数...)fprintf中(标准错误,参数)


I came across this one, don't understand.

#define IDEBUG(a...)  

What does the "(a...)" mean?

解决方案

That's a variadic macro.

Quoting verbatim from the linked page:

A macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of a function. Here is an example:

 #define eprintf(...) fprintf (stderr, __VA_ARGS__)

This kind of macro is called variadic. When the macro is invoked, all the tokens in its argument list after the last named argument (this macro has none), including any commas, become the variable argument. This sequence of tokens replaces the identifier VA_ARGS in the macro body wherever it appears. Thus, we have this expansion:

 eprintf ("%s:%d: ", input_file, lineno)
      ==>  fprintf (stderr, "%s:%d: ", input_file, lineno)

And for that specific form, quoting further down in the page:

If your macro is complicated, you may want a more descriptive name for the variable argument than __VA_ARGS__. CPP permits this, as an extension. You may write an argument name immediately before the `...'; that name is used for the variable argument. The eprintf macro above could be written

#define eprintf(args...) fprintf (stderr, args)

这篇关于这是什么的#define语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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