c-preprocessor

Confusing MACRO and enum definition

心不动则不痛 提交于 2019-12-01 21:57:35
I was browsing some Route netlink source code. I wanted to figure out what was the value of RTNLGRP_NEIGH Source: http://lxr.free-electrons.com/source/include/linux/rtnetlink.h?v=2.6.35#L550 541 /* RTnetlink multicast groups */ 542 enum rtnetlink_groups { 543 RTNLGRP_NONE, 544 #define RTNLGRP_NONE RTNLGRP_NONE 545 RTNLGRP_LINK, 546 #define RTNLGRP_LINK RTNLGRP_LINK 547 RTNLGRP_NOTIFY, 548 #define RTNLGRP_NOTIFY RTNLGRP_NOTIFY 549 RTNLGRP_NEIGH, 550 #define RTNLGRP_NEIGH RTNLGRP_NEIGH 551 RTNLGRP_TC, 552 #define RTNLGRP_TC RTNLGRP_TC 553 RTNLGRP_IPV4_IFADDR, 554 #define RTNLGRP_IPV4_IFADDR

Is there a way to control macro expansion order

雨燕双飞 提交于 2019-12-01 21:23:33
I am hoping that someone may have an idea on how to control/specify the order of macro expansion. Here is the context: // 32 bit increments, processor has registers for set, clear and invert #define CLR_OFF 1 #define SET_OFF 2 #define INV_OFF 3 #define SET(reg,bits) *((volatile unsigned long*)(& reg+SET_OFF)) = bits //Now if I use this I can do it quite nicely with #define STATUS_LED 0x0040; SET(LATB, STATUS_LED); // LATB is port of the LED. I've actually had to move hardware around quite a bit as of late so I decided to group the LATB info with the STATUS_LED like so... #define STATUS_LED

for loop macro coding style

爷,独闯天下 提交于 2019-12-01 21:08:00
One of my tutors at university suggests using macros to reduce repetition in c99 code, like this. #define foreach(a, b, c) for (int a = b; a < c; a++) #define for_i foreach(i, 0, n) #define for_j foreach(j, 0, n) #define for_ij for_i for_j Which can be used like this: for_ij { /*do stuff*/; } for_i { /*do stuff*/; } Another tutor with industrial background discourages its use, claiming it was seen as an anti-pattern at his former employer (but he does not know the reason behind this) . In fact by grepping through source code of large projects one rarely finds those constructs outside of short

Working of the C Preprocessor

点点圈 提交于 2019-12-01 21:06:36
How does the following piece of code work, in other words what is the algorithm of the C preprocessor? Does this work on all compilers? #include <stdio.h> #define b a #define a 170 int main() { printf("%i", b); return 0; } The preprocessor just replaces b with a wherever it finds it in the program and then replaces a with 170 It is just plain textual replacement. Works on gcc. It's at §6.10.3 (Macro Replacement): 6.10.3.4 Rescanning and further replacement 1) After all parameters in the replacement list have been substituted and # and ## processing has taken place, all placemarker

Statement in C++ macro

不想你离开。 提交于 2019-12-01 20:54:37
Reading chromium code, found helpful macro for handling EINTR errno of system calls on POSIX compliant systems. Here are the code(base/posix/eintr_wrapper.h): #define HANDLE_EINTR(x) ({ \ decltype(x) eintr_wrapper_result; \ do { \ eintr_wrapper_result = (x); \ } while (eintr_wrapper_result == -1 && errno == EINTR); \ eintr_wrapper_result; \ }) The question is what is the role of last statement in macro eintr_wrapper_result; ? If we use commas instead of semicolons - it will be clear - to return the result of last operation(comma operator). But what is purpose in this case? This macro uses the

Is it legal to pass the macro name to an X-Macro list

时光毁灭记忆、已成空白 提交于 2019-12-01 20:33:19
问题 It occurred to me that the following would be a preferable style of X-macro trick: #define LIST_OF_COLOURS(X) \ X(RED) \ X(GREEN) \ X(BLUE) #define LIST_OF_FRUIT(X) \ X(APPLE) \ X(ORANGE) \ X(TOMATO) Specifically, passing the X macro to the list, rather than undefining and redefining it every time the list is instantiated. This allows: #define X_LIST(x) x, #define X_STRING_LIST(x) #x, #define COMPREHENSIVE_SETUP(n, l) \ enum n { l(X_LIST) }; \ char const* n##Names[] = { l(X_STRING_LIST) };

C++ macro expansion, debugging

泄露秘密 提交于 2019-12-01 20:20:47
问题 I have a bunch of MACROS in C++ code that expand into some functions. And I am debugging something. Just want to see what the code ends up looking like” Any ideas? 回答1: You can view the preprocessor output to see what the code looks like after it is preprocessed. gcc and Visual C++ will both preprocess to stdout if you pass the -E flag on the command line to the compiler. 回答2: /P, see it under C++ - Preprocessor - Process to a File in the Visual Studio's Property Pages, the output file has

CPP/GPP in Fortran variadic macro (plus Fortran // concatenation)

老子叫甜甜 提交于 2019-12-01 20:14:40
I'm trying to compile a huge, world-renowned numerical weather prediction code - written mostly in Fortran 90 - that uses cpp extensively, and successfully, with PGI, Intel and gfortran. Now, I've inherited a version where experts have added several hundred cases of variadic macros. They use Intel and fpp , which is presumably a little more Fortran-centric, and can get it all to work. I need to use gfortran, and have not been able to get cpp to work on this code with its new additions. A gross simplification of the problem is as follows - Code to preprocess: PRINT *, "Hello" // "Don" #define

Conditional definition of elements in an X Macro

谁说我不能喝 提交于 2019-12-01 19:50:37
Imagine I have an X Macro for a list of items defined something like this: #define X_MACRO(FN) \ FN(foo) \ FN(bar) \ FN(zip) This works great and I can call it to generate the same code templatized for each element, like: #define xstr(s) str(s) #define str(s) #s #define PRINT_X(E) void print_ ## E () { std::cout << str(E); }; X_MACRO(PRINT_X) This generates functions like void print_foo() { std::cout << "foo"; }; for each of the X_MACRO elements. So far, so good. Now, however, I want the list of X Macro elements to be conditional on a pre-processor macro. For example the zip element should

How to guard move constructors for C++03 and C++11?

喜欢而已 提交于 2019-12-01 19:39:17
This is similar to What differences, if any, between C++03 and C++11 can be detected at run-time? . But in this case, I want detection to occur via the preprocessor. How should we guard the move constructor (and move assignment ) when the sources are used in both C++03 and C++11? Is the following sufficient (is move semantics something all C++ compilers adopted due to it being essential/core feature)? #if (__cpluplus >= 201103L) Foo(Foo&& other); #endif Or do I need to get into compiler specifics? If we need compiler specific macros, then how do we handle situations like Visual Studio 2012 _