宏中可变数量的参数 [英] Variable Number of Arguments in Macro

查看:76
本文介绍了宏中可变数量的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




任何人都可以解决以下代码的问题


代码:


#include" stdio.h"

#include" iostream.h"


void Temp(int a,char * str,.. 。)

{

//处理争论的代码

}


#define MYPRINT (_x_)printf _x_

#define MYPRINT1(_x_)Temp(10,_x_)

int main()

{

MYPRINT((这是对多个参数的测试%s

%d,__ FILE __,__ LINE__));

MYPRINT1( (这是对多个参数的测试%s

%d,__ FILE __,__ LINE__));


返回0;

}


问题:


在第一个宏中,我能够按照预期从
$获得结果b $ b printf在第二种情况下我的参数不正确

传递给函数Temp。谁能告诉我为什么我不是

abel使用宏来将参数传递给一个具有一些参数和参数变量数量的函数? br />

有什么方法可以像我预期的那样调整参数吗?怎么样?

谢谢

praveen

解决方案

Pr * *************@gmail.com 写道:

任何人都可以解决下面代码的问题

代码:

#include" stdio.h"
#include" iostream.h"

void Temp(int a,char * str,... )
//
//代码处理论证

#define MYPRINT(_x_)printf _x_
#define MYPRINT1(_x_)Temp( 10,_x_)



MYPRINT((这是对多个参数的测试%s
%d,__ FILE __,__ LINE__ ));
MYPRINT1((这是一个测试对于多个参数%s
%d",__ FILE __,__ LINE__));

返回0;
}

问题:

在第一个宏中,我能够从
printf得到预期的结果,在第二种情况下,我的参数没有正确地传递给函数Temp。你们中的任何人都可以告诉我为什么我不会使用宏来将参数传递给一个具有一些参数的必需数量和参数的可变数量的函数吗?


因为当宏MYPRINT1被替换时你会得到


Temp(10,(这是......,文件名 .ext",123));


参数周围的额外括号使它成为单个

表达式,带有两个运算符逗号而不是部分
$'$ b'参数''Temp''函数。顺便说一下,你得到123''char *''

预计。它很可能是未定义的行为。

有没有什么方法可以像我预期的那样调整参数?怎么样?




你最有可能的不可能。看看你的编译器是否支持variadic

宏。 (带省略号的宏)。


V

-

请在回复e时删除大写'''' -mail

我没有回复最热门的回复,请不要问


* Pr ************** @ gmail.com


任何人都可以解决以下代码的问题:


""问题?我数了很多问题。哪一个?


代码:

#include" stdio.h"


使用< headername>形式而不是headername对于标准标题。

这样你就可以避免在本地

目录中找到同名的标题。


#包括iostream.h。


这不是标准的标题,也不会用例如Visual C ++

7.1或更高。使用< iostream>代替。 <&的iostream GT;是标准的标题。


void Temp(int a,char * str,...)


第二个参数应声明为


char const * str


除非你希望函数Temp能够修改

的内容''str''。

省略号'''''通常不应该用在C ++代码中,因为

它是/危险/(非类型安全)和/有限/(没有非POD对象);

有更好的类型安全解决方案。


{
//处理参数的代码
}
#define MYPRINT(_x_)printf _x_
#define MYPRINT1(_x_)Temp(10,_x_)


一般来说,使用宏并不是一个好主意。看看这个组的常见问题和

Bjarne Stroustrup的C ++常见问题解答原因。

int main()
{
MYPRINT((这是对多个参数的测试%s
%d,__ FILE __,__ LINE__));
MYPRINT1((这是对多个参数的测试%s
%d",__ FILE __,__ LINE__));

返回0;
}

问题:

在第一个宏我可以从
printf得到预期的结果,在第二种情况下我的参数没有正确地传递给函数Temp。你们中的任何人都可以告诉我为什么我不会使用宏来将参数传递给一个具有一些参数的必需数量和参数的可变数量的函数吗?


第二次宏调用不起作用,因为它扩展为


Temp(10,(some text,__ FILE __,__ LINE__) );


这在语法上是无效的。


有没有什么方法可以像我预期的那样调整参数?怎么做?



不,不像你预期的那样。


解决方案取决于你想要达到的目标。显然它不是你的代码所说明的,因为通过在没有宏的情况下直接调用Temp可以更容易实现
。换句话说,

你已经说明了某个问题的一个有缺陷的解决方案,而不是

问题本身 - 为了解决这个问题,请解释一下。


我怀疑它与伐木或追踪有关吗?


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的事情是什么?


Alf P. Steinbach写道:

* 镨************** @ gmail.com :< blockquote class =post_quotes> [..]
第二次宏调用不起作用,因为它扩展为

Temp(10,(some some,__ FILE __,__ LINE__)) ;




真的吗?你的意思是__FILE__和__LINE__没有被取代?

为什么?你试过吗?

这在语法上是无效的。




为什么它无效?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回应top-发布回复,请不要问


Hi

Could anyone solve the problem for the code below

The Code:

#include "stdio.h"
#include "iostream.h"

void Temp( int a, char* str,...)
{
//code to handle the arguments
}

#define MYPRINT(_x_) printf _x_
#define MYPRINT1(_x_) Temp( 10,_x_)

int main()
{
MYPRINT(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));
MYPRINT1(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));

return 0;
}

Problem:

In the first macro I am able to get the result as expected from the
printf where as in the second case my parameters are not properly
passed to the function Temp. Could anyone of you tell me why i am not
abel to use the macro to pass parameter to a function with some
mandatory number of parameter and variable number of parameter?

Is there any way that i can paa the parameter as i expected?how?
Thanks
praveen

解决方案

Pr**************@gmail.com wrote:

Could anyone solve the problem for the code below

The Code:

#include "stdio.h"
#include "iostream.h"

void Temp( int a, char* str,...)
{
//code to handle the arguments
}

#define MYPRINT(_x_) printf _x_
#define MYPRINT1(_x_) Temp( 10,_x_)

int main()
{
MYPRINT(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));
MYPRINT1(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));

return 0;
}

Problem:

In the first macro I am able to get the result as expected from the
printf where as in the second case my parameters are not properly
passed to the function Temp. Could anyone of you tell me why i am not
abel to use the macro to pass parameter to a function with some
mandatory number of parameter and variable number of parameter?
Because when the macro MYPRINT1 is substituted you get

Temp( 10,("This is..","filename.ext",123));

The extra set of parentheses around the arguments makes it a single
expression with two operators comma instead of part of the list of
arguments to the ''Temp'' function. BTW, you get 123 where ''char*''
is expected. It''s most likely undefined behaviour.
Is there any way that i can paa the parameter as i expected?how?



You most likely cannot. See if your compiler supports "variadic
macros" (macros with ellipsis).

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


* Pr**************@gmail.com:


Could anyone solve the problem for the code below
"The" problem? I count a multitude of problems. Which one?

The Code:

#include "stdio.h"
Use the <headername> form instead of "headername" for standard headers.
That way you avoid picking up a header with the same name in a local
directory.

#include "iostream.h"
This is not a standard header, and won''t compile with e.g. Visual C++
7.1 or better. Use <iostream> instead. <iostream> is a standard header.

void Temp( int a, char* str,...)
The second argument should be declared as

char const* str

unless you want the function Temp to be able to modify the contents of
''str''.

The ellipsis ''...'' should generally not be used in C++ code, because
it''s /dangerous/ (not typesafe) and /limited/ (no non-POD objects);
there are much better typesafe solutions.

{
//code to handle the arguments
}

#define MYPRINT(_x_) printf _x_
#define MYPRINT1(_x_) Temp( 10,_x_)
Generally it''s not a good idea to use macros. See this group''s FAQ and
Bjarne Stroustrup''s C++ FAQ for reasons why.

int main()
{
MYPRINT(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));
MYPRINT1(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));

return 0;
}

Problem:

In the first macro I am able to get the result as expected from the
printf where as in the second case my parameters are not properly
passed to the function Temp. Could anyone of you tell me why i am not
abel to use the macro to pass parameter to a function with some
mandatory number of parameter and variable number of parameter?
The second macro invocation does not work because it expands to

Temp( 10, ("some text",__FILE__,__LINE__));

which is syntactically invalid.

Is there any way that i can paa the parameter as i expected?how?



No, not as you expected.

A solution depends on what you want to achieve. Obviously it''s not
what''s illustrated by your code, because that could be much more easily
achieved by calling Temp directly without the macro. In other words,
you have illustrated a flawed solution to some problem, instead of the
problem itself -- to get help with that problem, explain it.

I suspect, though, that it has to do with logging or tracing?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Alf P. Steinbach wrote:

* Pr**************@gmail.com:

[..]
The second macro invocation does not work because it expands to

Temp( 10, ("some text",__FILE__,__LINE__));



Really? You mean __FILE__ and __LINE__ do not get substituted?
Why? Have you tried it?
which is syntactically invalid.



Why is it invalid?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于宏中可变数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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