c-preprocessor

How to write safe and user friendly c/c++ #define macros

♀尐吖头ヾ 提交于 2019-12-22 08:36:27
问题 I have been thinking about how macros can be written to be safe, readable and intuitive. Proper use of them should be understood by the looks of them and when used incorrectly the compiler should tell you, and not let you introduce an obscure bug. When writing multiple line define macros I usually find myself constructing them like this to fullfill the desired criteria: #define macro(x) do{ \ ... some code line ; \ ... some code line ; \ }while(0) This way you can both... if (a) { macro(a); }

Pointer to #define

佐手、 提交于 2019-12-22 08:17:12
问题 I was just curious to know if it is possible to have a pointer referring to #define constant. If yes, how to do it? 回答1: The #define directive is a directive to the preprocessor, meaning that it is invoked by the preprocessor before anything is even compiled. Therefore, if you type: #define NUMBER 100 And then later you type: int x = NUMBER; What your compiler actually sees is simply: int x = 100; It's basically as if you had opened up your source code in a word processor and did a find

Doxygen Document All Conditional Defines

不羁的心 提交于 2019-12-22 07:56:07
问题 I have a project where I have a substantial amount of conditional defines for making cross platform development easier. However I'm having issues convincing Doxygen to extract all the defines, as it will only pick up ones that only happened to evaluate. For example in the following snippet, Doxygen will document TARGET_X86_64 but not TARGET_ARM64 . #if defined(_M_ARM64) || defined(__arm64__) || defined(__aarch64__) /** Build target is ARM64 if defined. */ #define TARGET_ARM64 #else /** Build

Doxygen Document All Conditional Defines

北慕城南 提交于 2019-12-22 07:56:02
问题 I have a project where I have a substantial amount of conditional defines for making cross platform development easier. However I'm having issues convincing Doxygen to extract all the defines, as it will only pick up ones that only happened to evaluate. For example in the following snippet, Doxygen will document TARGET_X86_64 but not TARGET_ARM64 . #if defined(_M_ARM64) || defined(__arm64__) || defined(__aarch64__) /** Build target is ARM64 if defined. */ #define TARGET_ARM64 #else /** Build

How to force gcc preprocessor to preserve whitespace?

纵饮孤独 提交于 2019-12-22 07:42:50
问题 For the code: int i; gcc preprocessor outputs: int i; How to force it to preserve whitespace? I call preprocessor with: gcc -E somefile.c command. 回答1: Use it in traditional mode, ie '-traditional-cpp' as described here. 来源: https://stackoverflow.com/questions/445986/how-to-force-gcc-preprocessor-to-preserve-whitespace

How to force gcc preprocessor to preserve whitespace?

早过忘川 提交于 2019-12-22 07:42:16
问题 For the code: int i; gcc preprocessor outputs: int i; How to force it to preserve whitespace? I call preprocessor with: gcc -E somefile.c command. 回答1: Use it in traditional mode, ie '-traditional-cpp' as described here. 来源: https://stackoverflow.com/questions/445986/how-to-force-gcc-preprocessor-to-preserve-whitespace

How to get Xcode 8 C preprocessor to ignore // comments in #defines

时间秒杀一切 提交于 2019-12-22 06:42:41
问题 The C preprocessor ( cpp ) seems like it should handle this code correctly: #define A 1 // hello there int foo[A]; I would expect to replace A with 1 . What happens is that A is replaced with 1 // hello there , which results in the following output from cpp -std=c99 test.c : # 1 "test.c" int foo[1 // hello there]; Which is not valid C and fails to compile. How can I get cpp to perform the proper replacement? Note on compiler: Using cpp from the latest (8.2.1, Dec 2016) Xcode on mac, so I

Why is post-compilation code injection a better idea than pre-compilation code injection?

一世执手 提交于 2019-12-22 06:37:01
问题 So we all know that C# doesn't have a C-like macro pre-processor (and there's a good thread on why here). But now that AOP is gaining traction, it seems like we're starting to do stuff with post-processors that we used to do with pre-processors (bear in mind that I am only getting my feet wet with PostSharp so am perhaps off base). I am a huge fan of attributes in C#, but if a pre-processor was left out for good reasons (which, as a former MFC user I still question but nevertheless accept)

#ifdef with multiple tokens, is this legal?

白昼怎懂夜的黑 提交于 2019-12-22 06:32:27
问题 Today I came across some C++ code that contains an #ifdef clause like this: #ifdef DISABLE_UNTIL OTHER_CODE_IS_READY foo(); #endif Note the space between "DISABLE_UNTIL" and "OTHER_CODE_IS_READY". Essentially there are two tokens specified in the #ifdef line. My question is, is this legal C++ code? (g++ compiles it without any errors, and it apparently just ignores the second token). And if it is legal, should the second token have any effect? 回答1: [C++11 16.1] , [C++11 16.5] and,

Initialize a 2D array of unknown size using macros in C

好久不见. 提交于 2019-12-22 05:57:10
问题 I was working on a small macro project that requires me to pass a 2 dimensional array literal to one of my macros like so: myMacro({{0, 1, 2}, {2, 1, 0}}) . Without having to pass the size of the array literal to the macro, is there a way to have it expand to the following: int[2][3] = { {0, 1, 2}, {2, 1, 0} } or something equivalent (any initialization that preserves the shape of the array will work)? Thanks in advance for any help 回答1: #include <boost/preprocessor/tuple/size.hpp> #include