preprocessor-directive

What is double evaluation and why should it be avoided?

夙愿已清 提交于 2019-11-27 01:44:12
问题 I was reading that in C++ using macros like #define max(a,b) (a > b ? a : b) can result in a 'double evaluation'. Can someone give me an example of when a double evaluation occurs and why it's bad? P.S.: Surprisingly I couldn't find any detailed explanation when googling for it except for an example in Clojure (which I can't understand). 回答1: Imagine you wrote this: #define Max(a,b) (a < b ? b : a) int x(){ turnLeft(); return 0; } int y(){ turnRight(); return 1; } then called it like this:

Can #if pre-processor directives be nested in C++?

怎甘沉沦 提交于 2019-11-26 22:45:46
问题 I have a question about Pre-processor directives in c++: For example: #ifndef QUESTION //some code here #ifndef QUESTION //some code here #endif #endif Can we use it in this way, and can the C++ compiler match the ifndef and endif in the right way? 回答1: Yes, we can. The #endif statement matches to the previous #if #ifdef or #ifndef etc for which there hasn't been a corresponding #endif . e.g. #if ----------| #if -----| | #endif ---| | #endif --------| 回答2: Yes, you can nest #if / #endif

Can gcc output C code after preprocessing?

这一生的挚爱 提交于 2019-11-26 12:13:52
I'm using an open source library which seems to have lots of preprocessing directives to support many languages other than C. So that I can study what the library is doing I'd like to see the C code that I'm compiling after preprocessing, more like what I'd write. Can gcc (or any other tool commonly available in Linux) read this library but output C code that has the preprocessing converted to whatever and is also readable by a human? mipadi Yes. Pass gcc the -E option. This will output preprocessed source code. tpdi cpp is the preprocessor. Run cpp filename.c to output the preprocessed code,

Escaping a # symbol in a #define macro?

前提是你 提交于 2019-11-26 06:44:24
问题 Without going into the gory details I want to use a #define macro that will expand to a #include but the \'#\' sign is confusing the preprocessor (as it thinks I want to quote an argument.) For example, I want to do something like this: #define MACRO(name) #include \"name##foo\" And use it thus: MACRO(Test) Which will expand to: #include \"Testfoo\" The humble # sign is causing the preprocessor to barf. MinGW gives me the following error: \'#\' is not followed by a macro parameter I guess I

Can gcc output C code after preprocessing?

醉酒当歌 提交于 2019-11-26 02:32:56
问题 I\'m using an open source library which seems to have lots of preprocessing directives to support many languages other than C. So that I can study what the library is doing I\'d like to see the C code that I\'m compiling after preprocessing, more like what I\'d write. Can gcc (or any other tool commonly available in Linux) read this library but output C code that has the preprocessing converted to whatever and is also readable by a human? 回答1: Yes. Pass gcc the -E option. This will output

#ifdef replacement in the Swift language

一笑奈何 提交于 2019-11-25 23:23:39
问题 In C/C++/Objective-C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors. #ifdef DEBUG // Debug-only code #endif Is there a similar solution in Swift? 回答1: Yes you can do it. In Swift you can still use the "#if/#else/#endif" preprocessor macros (although more constrained), as per Apple docs. Here's an example: #if DEBUG let a = 2 #else let a = 3 #endif Now, you must set the "DEBUG" symbol elsewhere, though. Set