有没有一种方法可以为函数创建预处理器宏? [英] Is there a way to create a preprocessor macro for a function?

查看:65
本文介绍了有没有一种方法可以为函数创建预处理器宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以基于函数结果创建C ++预处理程序宏?

Is it possible to create a C++ preprocessor macro based on a function result?

例如,我想在预处理程序宏定义中动态保存屏幕高度:

For example, I'd like to save the screen height dynamically in a preprocessor macro definition:

#define SCREEN_HEIGHT   GetSystemMetrics(SM_CYVIRTUALSCREEN)

然后我要使用结果根据屏幕高度设置值:

Then I want to use the result to set values based on the screen height:

#if SCREEN_HEIGHT < 1200
    #define TOP_COORD     200
    #define BOTTOM_COORD  500
    #define LEFT_COORD    0
    #define RIGHT_COORD   1280
#else
    #define TOP_COORD     1100
    #define BOTTOM_COORD  1400
    #define LEFT_COORD    0
    #define RIGHT_COORD   1280
#endif

这不起作用,因为似乎未正确定义SCREEN_HEIGHT.

This doesn't work, as SCREEN_HEIGHT doesn't seem to be getting defined properly.

是否有更好的方法来做到这一点?这有可能吗?我希望能够在头文件中获取此屏幕高度信息,因为这是大量旧代码的一部分.

Is there a better way to accomplish this? Is this even possible? I want to be able to get this screen height info in the header file if possible, as this is part of a large chunk of legacy code.

推荐答案

否.宏在编译时就进行了完全评估,至少在概念上至少要由预处理器在实际代码进行编译之前进行评估,并且#if等中的表达式必须是预处理器表达式.在程序运行之前,无法知道屏幕的高度.

No. Macros are fully evaluated at compile time, conceptually at least by a pre-processor, before the actual code is even compiled, and the expressions in #if, etc. must be preprocessor expressions. The height of the screen cannot be known until the program runs.

这篇关于有没有一种方法可以为函数创建预处理器宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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