便携式宏 [英] portable typeof macro

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

问题描述




是否有一个等效的类型宏/方法来确定运行时和/或运算符中的

变量的类型。最重要的是,它与大多数已知的b
C编译器一起使用?


gcc编译器支持typeof()宏,但相同的代码没有得到

在solaris forte编译器和microsoft VS 2003编译器中编译。我尝试了下面的



#include< stdio.h>


int

main()

{

int i;

typeof(i)j = 10;

i = j + 1;

printf("%d,%d \ n",i,j);

返回0;

}


上面的代码只是一个测试,看看编译器是否支持类型

宏/方法。但只有gcc支持它。然后我尝试了__typeof__

在ISO标准中描述我希望(我不确定顺便说一下),但是再次

由gcc而不是其他编译器支持。


这种类型的宏如何工作?或者是否有任何等效的

方法/宏可移植或可以随身携带工作

与所有知名编译器。


提前致谢。


最好的问候

RKK

解决方案

2006年12月25日21:52:07 -0800,rkk < te ******* @ yahoo.comwrote in

comp.lang.c:





是否有一个等效的类型的宏/方法来确定运行时和/或运算符中的一个

变量的类型。最重要的是,它适用于大多数已知的b $ b $ C编译器吗?



不,没有这样的运算符。


gcc编译器支持typeof()宏,但是相同的代码没有得到在solaris forte编译器和microsoft VS 2003编译器中编译的
。我尝试了下面的



#include< stdio.h>


int

main()

{

int i;

typeof(i)j = 10;

i = j + 1;

printf("%d,%d \ n",i,j);

返回0;

}



C中没有这样的东西,也没有需要这样的东西在

C.


上面的代码只是一个测试,看看编译器是否支持类型

宏/方法。但只有gcc支持它。然后我尝试__typeof__,其中

在ISO标准中描述我希望(我不确定顺便说一句),但是再次

由gcc而不是其他编译器支持。



不,没有__typeof__,或任何等价物。


这种类型的宏如何工作?或者是否有任何等价物



我们不知道它是如何工作的,因为它不是C $ / $
语言的一部分。但是,它必须基于编译器

所具有的信息,因此程序员也可以 - 或者可以拥有 -


方法/宏,它是便携式的,或者可以随身携带工作

与所有知名的编译器。



没有这样的东西,但是再次确实没有必要在B中这样的东西。
< blockquote class =post_quotes>
先谢谢。



你正在寻找一些不存在的东西,所以你不会去找b $ b来找到它。你应该做的是解释你正在尝试的问题

来解决,你认为你需要这样的功能会有所帮助。那么

我们可以建议解决问题的方法。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答for

comp.lang.c http://c-faq.com /

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~ a ... FAQ-acllc.html


" rkk" < te ******* @ yahoo.comwrites:


是否有等效的宏/方法来确定
$的类型b $ b运行时变量&最重要的是,它与大多数已知的b
C编译器一起使用?


gcc编译器支持typeof()宏,但相同的代码没有得到

在solaris forte编译器和microsoft VS 2003编译器中编译。我用b $ b尝试如下:



[snip]


" typeof"不是宏观;它是一个特定于gcc的关键字。标准C中不存在
,并且没有可移植的方式在C中实现它。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/ ~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Jack Klein< ja ******* @ spamcop.netwrites:


2006年12月25日21:52:07 -0800,rkk < te ******* @ yahoo.comwrote in

comp.lang.c:


>

是否有一个等效的类型的宏/方法来确定运行时和/或变量的变量类型。最重要的是,它适用于大多数已知的C编译器?



不,没有这样的运算符。


> gcc编译器支持typeof()宏,但是相同的代码没有在solaris forte编译器和microsoft VS 2003编译器中编译。我尝试了类似下面的内容:

#include< stdio.h>

int
main()
{
int i;
typeof(i)j = 10;
i = j + 1;
printf("%d,%d \ n",i,j);
返回0;
}



在C中没有这样的东西,也没有需要这样的东西在

C.



[...]


显然没有绝对需要,因为如果没有它,我们已经多年没有了b $ b。但它在某些情况下可能会有用。

例如,您可以使用它来编写类型 - 通用交换宏,

类似于(未经测试的代码如下):


#define SWAP(a,b)做{\

typeof(a)tmp = a; b = a; a = tmp; \\ /

}而(0)


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Hi,

Is there an equivalent typeof macro/method to determine the type of a
variable in runtime & most importantly that works well with most known
C compilers?

gcc compiler supports typeof() macro, but the same code is not getting
compiled in solaris forte compiler and in microsoft VS 2003 compiler. I
tried something like below:

#include <stdio.h>

int
main()
{
int i;
typeof(i) j = 10;
i = j + 1;
printf("%d,%d\n",i,j);
return 0;
}

The above code is just a test to see if the compiler supports typeof
macro/method. But only gcc supports it. I then tried __typeof__ which
is described in ISO standards I hope (I''m not sure btw), but again
supported by gcc and not by other compilers.

How does this typeof macro work? Or is there any equivalent
method/macro which is portable or that can be made portable to work
with all well known compilers.

Thanks in advance.

Best Regards
RKK

解决方案

On 25 Dec 2006 21:52:07 -0800, "rkk" <te*******@yahoo.comwrote in
comp.lang.c:

Hi,

Is there an equivalent typeof macro/method to determine the type of a
variable in runtime & most importantly that works well with most known
C compilers?

No, there is no such operator.

gcc compiler supports typeof() macro, but the same code is not getting
compiled in solaris forte compiler and in microsoft VS 2003 compiler. I
tried something like below:

#include <stdio.h>

int
main()
{
int i;
typeof(i) j = 10;
i = j + 1;
printf("%d,%d\n",i,j);
return 0;
}

There is no such thing in C, nor is there any need for such a thing in
C.

The above code is just a test to see if the compiler supports typeof
macro/method. But only gcc supports it. I then tried __typeof__ which
is described in ISO standards I hope (I''m not sure btw), but again
supported by gcc and not by other compilers.

No, there is no __typeof__, or anything equivalent.

How does this typeof macro work? Or is there any equivalent

We have no idea how it works here, because it is not part of the C
language. However, it must be based on information that the compiler
has, and therefore the programmer has -- or can have -- as well.

method/macro which is portable or that can be made portable to work
with all well known compilers.

There is no such thing, but then again there really is no need for
such a thing in C.

Thanks in advance.

You are looking for something that does not exist, so you aren''t going
to find it. What you should do is explain the problem you are trying
to solve, that you think you need such a feature would help with. Then
we can suggest ways of solving the problem.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


"rkk" <te*******@yahoo.comwrites:

Is there an equivalent typeof macro/method to determine the type of a
variable in runtime & most importantly that works well with most known
C compilers?

gcc compiler supports typeof() macro, but the same code is not getting
compiled in solaris forte compiler and in microsoft VS 2003 compiler. I
tried something like below:

[snip]

"typeof" is not a macro; it''s a gcc-specific keyword. It doesn''t
exist in standard C, and there''s no portable way to implement it in C.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Jack Klein <ja*******@spamcop.netwrites:

On 25 Dec 2006 21:52:07 -0800, "rkk" <te*******@yahoo.comwrote in
comp.lang.c:

>Hi,

Is there an equivalent typeof macro/method to determine the type of a
variable in runtime & most importantly that works well with most known
C compilers?


No, there is no such operator.

>gcc compiler supports typeof() macro, but the same code is not getting
compiled in solaris forte compiler and in microsoft VS 2003 compiler. I
tried something like below:

#include <stdio.h>

int
main()
{
int i;
typeof(i) j = 10;
i = j + 1;
printf("%d,%d\n",i,j);
return 0;
}


There is no such thing in C, nor is there any need for such a thing in
C.

[...]

Obviously there''s no absolute need for it, since we''ve gotten along
for many years without it. But it could be useful in some contexts.
For example, you could use it to write a type-generic swap macro,
something like (untested code follows):

#define SWAP(a, b) do { \
typeof(a) tmp = a; b = a; a = tmp; \
} while(0)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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