#if sizeof ... [英] #if sizeof...

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

问题描述

sizeof()在#if预处理器行中是否合法?


具体来说,我想使用以下健全性检查。在编译时

时间,但我在#if行的编译中遇到错误。


struct foo {

int a;

int b;

};


#if sizeof(struct foo)!= 8

#pragma error" sizeof foo不是8!"

#endif


(假设#pragma error"生成一个编译时错误。)


是的,我知道这样的事情的危险和不可移植性,

但它是'在一个低级TCP / IP通信模块中,我只想在编译时添加一个完整性检查,我不在一个平台上

结构与通讯协议不符。

-

+ ------------------ ------- + -------------------- + --------------------- -------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>

Is sizeof() legal within a #if preprocessor line?

Specifically, I would like to use the following "sanity check" at compile
time, but I''m getting an error on the compile about the #if line.

struct foo {
int a;
int b;
};

#if sizeof(struct foo) != 8
#pragma error "sizeof foo is not 8!"
#endif

(Assuming the "#pragma error" generates a compile-time error.)

Yes, I know all about the dangers and non-portability of things like this,
but it''s in a low-level TCP/IP communications module, and I''d just like to
add a sanity check at compile time that I''m not on a platform where the
structs don''t match the communications protocol.
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>

推荐答案

2005年1月14日星期五12:57:56 -0500,Kenneth Brody写道:
On Fri, 14 Jan 2005 12:57:56 -0500, Kenneth Brody wrote:
sizeof()在#if预处理器行中是否合法?


No.

具体来说,我想使用以下健全性检查。在编译的时候,但是我在编译#if行时遇到了错误。

struct foo {
int a;
int b;
};

#if sizeof(struct foo)!= 8


另一个问题是struct foo在<期间不存在br />
预处理阶段。

#pragma错误" sizeof foo不是8!
#endif

(假设 #pragma error"生成一个编译时错误。)

是的,我知道所有关于
这样的事情的危险和不可移植性,但是它处于低位 - 级别TCP / IP通信模块,我只是想在编译时添加一个健全性检查我不在平台上结构与通信协议不匹配。
Is sizeof() legal within a #if preprocessor line?
No.
Specifically, I would like to use the following "sanity check" at compile
time, but I''m getting an error on the compile about the #if line.

struct foo {
int a;
int b;
};

#if sizeof(struct foo) != 8
Another problem would be that struct foo doesn''t exist during
preprocessing phases.
#pragma error "sizeof foo is not 8!"
#endif

(Assuming the "#pragma error" generates a compile-time error.)

Yes, I know all about the dangers and non-portability of things like
this, but it''s in a low-level TCP/IP communications module, and I''d just
like to add a sanity check at compile time that I''m not on a platform
where the structs don''t match the communications protocol.




如果您希望表示匹配,那么问题比

只是大小还要多。


劳伦斯



If you''re expecting representation to match there are more issues than
just size.

Lawrence


文章< 41 *************** @ spamcop.net>,

Kenneth Brody< ke ****** @ spamcop.net>写道:
In article <41***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
sizeof()在#if预处理器行中是否合法?


编号预处理器不知道这些事情。

我只想在编译时添加一个完整性检查时间,我不在一个
结构与通信协议不匹配的平台上。
Is sizeof() legal within a #if preprocessor line?
No. The preprocessor doesn''t know about such things.
I''d just like to
add a sanity check at compile time that I''m not on a platform where the
structs don''t match the communications protocol.




你可以使用各种结构如果大小错误,则产生

编译时错误。例如:


char zzz [1 - 2 *(sizeof(struct foo)!= 8)];


但错误信息不会很有启发性。


- Richard



There are various constructs that you could use to produce
compile-time errors if the size is wrong. For example:

char zzz[1 - 2*(sizeof(struct foo) != 8)];

But the error message will not be very enlightening.

-- Richard


Richard Tobin< ri ***** @ cogsci.ed .ac.uk>写道:
Richard Tobin <ri*****@cogsci.ed.ac.uk> wrote:
char zzz [1 - 2 *(sizeof(struct foo)!= 8)];


一个非常可爱的主意!我偷了它,谢谢你!


#define CCASSERT(谓词)_x_CCASSERT_LINE(谓词,__LINE__)

[...]

#define _x_CCASSERT_LINE_CAT(谓词,行)\

typedef char constraint_violated_on_line _ ## line [2 *((谓词)!= 0)-1];


由于typedef声明没有创建对象,声明

要求`predicate''是一个常量(标量)表达式。另一个

等效的可能性是替换typedef使用extern。

但错误信息不会很有启发性。
char zzz[1 - 2*(sizeof(struct foo) != 8)];
A very cute idea! I''ve stolen it, thank you!

#define CCASSERT(predicate) _x_CCASSERT_LINE(predicate, __LINE__)
[...]
#define _x_CCASSERT_LINE_CAT(predicate, line) \
typedef char constraint_violated_on_line_##line[2*((predicate)!=0)-1];

Due to typedef declaration no object is created, and the declaration
requires `predicate'' to be a constant (scalar) expression. Another
equivalent possibility would be to replace "typedef" with "extern".
But the error message will not be very enlightening.




来自gcc的消息足以提供信息:

constr.c:30:数组大小`constraint_violated_on_line_30''是否定的


-

Stan Tobias

mailx`echo si***@FamOuS.BedBuG.pAlS.INVA LID | sed s / [[:upper:]] // g`



The message from gcc is enough informative:
constr.c:30: size of array `constraint_violated_on_line_30'' is negative

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`


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

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