游戏开发论坛

 找回密码
 立即注册
搜索
查看: 6222|回复: 11

[新手]VC2003中能不能定义变长参数的宏?

[复制链接]

53

主题

241

帖子

252

积分

中级会员

Rank: 3Rank: 3

积分
252
发表于 2006-11-10 05:45:00 | 显示全部楼层 |阅读模式
如题

8

主题

716

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2006-11-10 09:50:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

cannot

4

主题

66

帖子

66

积分

注册会员

Rank: 2

积分
66
发表于 2006-11-10 11:34:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

楼上简单明了.
偶来说得具体一点...

__cdecl    C和C++程序的缺省调用规范
在被调用函数(Callee)返回后,由调用者(Caller)调整堆栈。
调用者
    // call function
    // adjust stack
被调用函数
    // do work
    // return
函数的参数个数可变(就像printf函数一样),因为只有调用者才知道它传给被调用函数几个参数,才能在调用结束时适当地调整堆栈。

________________________________________________________________

__stdcall   
为了使用这种调用规范,需要你明确的加上__stdcall(或WINAPI)文字。即return-type __stdcall function-name[(argument-list)]
在被调用函数(Callee)返回前,由被调用函数(Callee)调整堆栈。
调用者
    // call function
被调用函数
    // do work
    // adjust stack
    // return
函数的参数个数不能是可变的。






36

主题

1047

帖子

1147

积分

金牌会员

Rank: 6Rank: 6

积分
1147
发表于 2006-11-10 11:51:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

宏是什么?宏代码什么时候决定?

8

主题

716

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2006-11-10 13:23:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

请自行翻看编译原理
代码在编译前会由预处理器将macro转换过去

6

主题

95

帖子

103

积分

注册会员

Rank: 2

积分
103
发表于 2006-11-11 16:16:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

好像是说宏,不是说函数。看有没有__VA_ARGS__,查查MSDN吧。2005有的,03不清楚。

9

主题

688

帖子

688

积分

高级会员

Rank: 4

积分
688
发表于 2006-11-11 16:24:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

可以用内联函数

0

主题

8

帖子

12

积分

新手上路

Rank: 1

积分
12
发表于 2006-11-13 19:24:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

宏只是简单的进行文本替换,不存在所谓的运行时特征(函数有),而变长参数的实现,是需要根据某些变量和栈的情况动态处理的(运行时处理),所以宏实现不了变长参数。

8

主题

310

帖子

311

积分

中级会员

Rank: 3Rank: 3

积分
311
QQ
发表于 2006-11-13 20:08:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

宏仅仅是编译时替换的

6

主题

95

帖子

103

积分

注册会员

Rank: 2

积分
103
发表于 2006-11-14 13:12:00 | 显示全部楼层

Re:[新手]VC2003中能不能定义变长参数的宏?

确实有变长参数的宏(vs2005):
To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.

The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma. The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.

Example
  Copy Code
// variadic_macros.cpp
#include <stdio.h>
#define EMPTY

#define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); }
#define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); }
#define CHECK3(...) { printf(__VA_ARGS__); }
#define MACRO(s, ...) printf(s, __VA_ARGS__)

int main() {
   CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)\n");
   CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)\n");   // won't print

   CHECK2(0, "here %s %s %s", "are", "some", "varargs2(3)\n");   // won't print
   CHECK2(1, "here %s %s %s", "are", "some", "varargs2(4)\n");

   // always invokes printf in the macro
   CHECK3("here %s %s %s", "are", "some", "varargs3(5)\n");

   MACRO("hello, world\n");
   // MACRO("error\n", EMPTY);   would cause C2059
}

这个__VA_ARGS__还挺有用,比如,你的宏参数是:vector<T,A>,就麻烦了,因为T与A间有“,”,此时就需要__VA_ARGS__。当然,其使用不可能像变参函数那样灵活。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-20 15:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表