c-preprocessor

Remove cast from constant in preprocessor

折月煮酒 提交于 2020-01-11 05:19:05
问题 Background In a microcontroller code, I am using a library provided by the producer where there are many constants defined. I'm trying to give an error if there's a mismatch between some of my constants (shared with components outside the microcontroller, with git-subtree ) and the microcontroller constants. For example, the library defines: #ifdef SOME_PARTICULAR_MODEL #define FLASH_BLOCK_SIZE ((uint8_t)64) /* else, other models */ #endif And somewhere, in a header shared between the

How to define preprocessor macro to check iOS version

本秂侑毒 提交于 2020-01-10 19:41:51
问题 I use it to check iOS version, but it doesn't work: #ifndef kCFCoreFoundationVersionNumber_iPhoneOS_5_0 #define kCFCoreFoundationVersionNumber_iPhoneOS_5_0 675.000000 #endif #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0 #define IF_IOS5_OR_GREATER(...) \ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_5_0) \ { \ __VA_ARGS__ \ } #else #define IF_IOS5_OR_GREATER 0 #endif when I make #if IF_IOS5_OR_GREATER NSLog(@"iOS5"); #endif nothing happens. Is something

How to declare/define interdependent templates in C++?

偶尔善良 提交于 2020-01-10 04:33:08
问题 Usually in C++ when I need interdependencies between classes, I use forward declarations in the header files and then include both header files in each cpp file. However this approach breaks when working with templates. Because templates have to be entirely in the header files (not counting the case when I put the code into cpp and enumerate template class A<T>; for each supported T - this is not always feasible, e.g. when T is a lambda). So is there a way to declare/define interdependent

C++ assert: the precedence of the expression in an assert macro

断了今生、忘了曾经 提交于 2020-01-10 03:33:08
问题 In C++: assert( std::is_same<int , int>::value ); // does not compile assert( (std::is_same<int , int>::value) ); // compiles Can anyone explain why? 回答1: The comma is being treated as a argument separator for the macro, but parenthesis in your second case protect the arguments. We can see this by going to the draft C++ standard section 16.3 Macro replacement which says ( emphasis mine ): The sequence of preprocessing tokens bounded by the outside-most matching parentheses forms the list of

How efficient is define in PHP?

喜你入骨 提交于 2020-01-09 19:46:35
问题 C++ preprocessor #define is totally different. Is the PHP define() any different than just creating a var? define("SETTING", 0); $something = SETTING; vs $setting = 0; $something = $setting; 回答1: 'define' operation itself is rather slow - confirmed by xdebug profiler. Here is benchmarks from http://t3.dotgnu.info/blog/php/my-first-php-extension.html: pure 'define' 380.785 fetches/sec 14.2647 mean msecs/first-response constants defined with 'hidef' extension 930.783 fetches/sec 6.30279 mean

Does the program execution always start from main in C?

老子叫甜甜 提交于 2020-01-09 10:33:15
问题 Must program execution start from main, or can the starting address be modified? #include <stdio.h> void fun(); #pragma startup fun int main() { printf("in main"); return 0; } void fun() { printf("in fun"); } This program prints in fun before in main . 回答1: The '#pragma' command is specified in the ANSI standard to have an arbitrary implementation-defined effect. In the GNU C preprocessor, '#pragma' first attempts to run the game 'rogue'; if that fails, it tries to run the game 'hack'; if

A #define in C with three dots

二次信任 提交于 2020-01-09 10:06:50
问题 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__)) This is definition for these 2 macros; later in the code LOGI and LOGW are used this way LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); and this way LOGW("Unable to eglMakeCurrent"); Since I try to avoid complex macros and #define in general, I

A #define in C with three dots

99封情书 提交于 2020-01-09 10:06:34
问题 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__)) This is definition for these 2 macros; later in the code LOGI and LOGW are used this way LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); and this way LOGW("Unable to eglMakeCurrent"); Since I try to avoid complex macros and #define in general, I

How do I show the value of a #define at compile-time?

主宰稳场 提交于 2020-01-09 08:25:25
问题 I am trying to figure out what version of Boost my code thinks it's using. I want to do something like this: #error BOOST_VERSION but the preprocessor does not expand BOOST_VERSION. I know I could print it out at run-time from the program, and I know I could look at the output of the preprocessor to find the answer. I feel like having a way of doing this during compilation could be useful. 回答1: I know that this is a long time after the original query, but this may still be useful. This can be

Seeing expanded C macros

我的梦境 提交于 2020-01-08 19:43:15
问题 If I want to expand a C macro, what are some good ways to do that (besides tracing it manually)? For instance, GTK_WIDGET_SET_FLAGS , it uses a macro that uses a macro that uses a macro (or two) ... I want to just see it somehow expanded automagically, instead of searching for every macro, every step of the way. UPDATE I tried cpp, but it seemed to only do the first pass on: GTK_WIDGET_SET_FLAGS(obj, 13) I got the include file expanded, and then: G_STMT_START{ ((GTK_OBJECT_FLAGS (obj)) |= (13