c-preprocessor

Macro that accept new object

99封情书 提交于 2019-12-11 12:57:27
问题 In my code, I have: #define EV( event ) SendEvent( new event ); EV( evFormat ); But I want to pass a created object in the EV macro, like: CEvent *ev = new CEvent(); EV( ev ); Is this possible? Because there is no way I will modify the EV macro. 回答1: #define EV( event ) SendEvent( new event ); // Can't be changed. The macro enforces that each call to SendEvent should create a new dynamic object. Your problem is not just that using a macro for that is stupid and e.g. reduces the readability of

C++03 linker “already defined symbol” doesn't appear on intermediate file

你。 提交于 2019-12-11 12:19:28
问题 I am having a problem with a large project on visual studio 2005 on which I have run out of ideas. I can't even put a working code snippet because I don't know what's related, but I will try: I needed to make each .cpp file in my project have its own ID number, and create an instance of an object (which is globally accessible) that knows that ID. I followed the help on the accepted answer on this thread How to manage file unique IDs in c++ and made it work in a sandbox environment. Adding

Array of macros in c — is it possible

拜拜、爱过 提交于 2019-12-11 12:18:39
问题 I was wondering if it is possible to create something like an array of macros. I've implemented the following code which works: struct led_cmds_ { ioport_pin_t *commands[LED_COUNT] ; } ; struct led_cmds_ the_led_cmd_ ; void populate() { the_led_cmd_.commands[0] = SPECIFICPIN(0); } and in main: int main(void) { //..... populate(); LED_On(the_led_cmd_.commands[0]); } SPECIFICPIN(x) is macro defined as: #define SPECIFICPIN(X) (LED##X##_PIN) What I was hoping for is a way to is a way to do

Calling a function from a macro

∥☆過路亽.° 提交于 2019-12-11 12:12:50
问题 Hi I am trying to create a macro that computes the base 2 logarithm of a number in C. The number is supposed to be the size of a table that is #defined also as seen below. I searched around and found this site that contains an implementation of base2log https://graphics.stanford.edu/~seander/bithacks.html uint8_t base2log(uint16_t value){ uint8_t result = 0; // r will be lg(v) while (value >>= 1) { result++; } return result; } My first idea is to now create the macro as: #define SIZE 256

How to suppress #define locally?

假如想象 提交于 2019-12-11 10:52:38
问题 Just caught a silly bug. I have a zip processing library with a CreateFile() function in it. Winbase.h , included somewhere deep in my headers, redefines it as CreateFileW and linker goes nuts. Of course I will exclude winbase in this particular case. It just shouldn't be in the scope in the first place. But the theoretical question is still interesting, Is there a way to suppress some defines locally? 回答1: Removing the offending header file is ALWAYS the best solution for this (especially

C Macro to protect definitions

笑着哭i 提交于 2019-12-11 09:09:24
问题 Is there a way to protect a macro definition? To be more specific consider the following: #define macro1 x // code segment 1 involving macro1 goes here // code segment 2 involving macro1 goes here // code segment 3 involving macro1 goes here In the example I have placed 3 comments denoting code segments involving the macro. What I would like to do now is to be able to avoid having the macro affect code segment 2. Is there a way to tell the compiler to replace all instances of macro1 in

Why do I get different results when using a function versus a macro?

十年热恋 提交于 2019-12-11 08:07:51
问题 I'm using DevCPP IDE and I found that while programming in c, Value returned by: float f(float x) { return 1/(1+x*x); } and value returned by f(x) if it's defined as: #define f(x) 1/(1+x*x) are different. Why am I getting different results in these cases? EDIT: Here's my code for which I'm getting the anomaly: main() { int i; float a=0, b=1, h, n=12, s1, s2=0; h=(b-a)/n; //step length s1=f(a)+f(b); for(i=1;i<=n-1;i++) { s2+=f(a+(i*h)); } s1=(s1+2*s2)*h/2; printf("sum: %f", s1); getch(); }

Is there a C preprocessor that eliminates #ifdefs but also evaluates preprocessor macros?

こ雲淡風輕ζ 提交于 2019-12-11 07:13:25
问题 I'm having a project that does lots of this #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) // do some legacy stuff #else // do current stuff #endif where KERNEL_VERSION is defined as #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) I'd like to eliminate the defines that are not relevant for the current version, but tools like sunifdef don't evaluate the KERNEL_VERSION macro, so something like sunifdef --replace -DKERNEL_VERSION\(a,b,c\)=\(\(\(a\)\<\<16\)+\(\(b\)\<\<8\)+\(c\)\)

make preprocessor trace the source of a definition

杀马特。学长 韩版系。学妹 提交于 2019-12-11 06:29:28
问题 In a large and complex pile of source (not invented here, hacked together by Elbonian code-slaves) it can be the case that several bits of code have their own local duplicate of some common header file in their path. Due to the many layers of build and use of guard macros to prevent re-definition, a value #defined in one place may therefore be remembered by the compiler and used elsewhere despite a more "local" header #defining the same thing. My question is : Can I get the C preprocessor to

Why #define is bad? [duplicate]

安稳与你 提交于 2019-12-11 06:24:37
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When are C++ macros beneficial? Why is #define bad and what is the proper substitute? Someone has told me that #define is bad. Well, I honestly don't not understand why its bad. If its bad, then what other way can I do this then? #include <iostream> #define stop() cin.ignore(numeric_limits<streamsize>::max(), '\n'); 回答1: #define is not inherently bad . However, there are usually better ways of doing what you