编译时的sizeof条件 [英] Compile-time sizeof conditional

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

问题描述

我想如果条件涉及的sizeof 来定义一个宏是真实的,什么也不做(但仍编译)如果是假的。如果preprocessor支持的sizeof ,它应该是这样的:

I want to define a macro if a condition involving sizeof is true and do nothing (but still compile) if it is false. If the preprocessor supported sizeof, it would look like this:

#if (sizeof(void*) <= sizeof(unsigned int)) // what goes here?
#  define POINTER_FITS_INTO_UINT
#endif

有一些网页(例如:<一href=\"http://scaryreasoner.word$p$pss.com/2009/02/28/checking-sizeof-at-compile-time/\">http://scaryreasoner.word$p$pss.com/2009/02/28/checking-sizeof-at-compile-time/)这解释了如何使一个编译时的断言在 的sizeof的(和无法编译如果失败),但我不明白的方式这种方法扩展到我想要的东西。

There are some pages (e.g. http://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/) which explain how to make a compile-time assertion on sizeof (and fail to compile if it fails), but I don't see a way to extend this approach to what I want.

推荐答案

您就不能做到这一点。的sizeof是一个编译时运营商。 #if和#define和preprocessor有关。由于preprocessor运行编译器之前这才将无法工作。您可能,但是,能够找到一个神秘的编译器开关,让你多传递(即preprocess,pretend编译,preprocess,编译),但是,平心而论,我放弃试图做你想要什么。它不是为了工作,简单地说,它没有。

You just can't do it. sizeof is a compile time operator. #if and #define and preprocessor related. As the preprocessor runs BEFORE the compiler this just won't work. You may, however, be able to find an arcane compiler switch that will allow you to multi pass it (ie preprocess, pretend compile, preprocess, compile) but, in all fairness, I'd give up trying to do what you want. Its not meant to work and, simply, it doesn't.

您最好最好是设置这样的定义为传递到编译器-D命令。可以静态断言选择的那些是正确的。这样,你只需要在外部设置的一些定义为给定的编译模式(如PowerPC的版本)等。

Your best best is to set such defines as -D commands passed to the compiler. You can statically assert that the ones chosen are correct. This way you just have to set up a few defines externally for a given compile mode (eg PowerPC Release) and so on.

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

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