Compile-Time Function Execution

廉价感情. 提交于 2021-02-11 05:54:49

问题


Is there a way to perform compile-time function execution in C? With GCC? I've only seen this available using constexpr in C++.


回答1:


As long as there are only constants involved in an expression, it will get calculated at compile-time. C++ constexpr is mostly a type safe way of doing so without involving macros. In C, there are only macros. For example:

#define CIRCLE_AREA(r) (int32_t)( (double)(r) * (double)(r) * M_PI )

int32_t area = CIRCLE_AREA(5);

performs all calculations at compile-time, so it is equivalent to writing:

int32_t area = 78;


来源:https://stackoverflow.com/questions/36418757/compile-time-function-execution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!