c-preprocessor

C block becomes expression: ( {int a = 1; int b = 2; a+b;} ) equals 3

痞子三分冷 提交于 2019-12-09 18:20:34
问题 While reading http://en.wikipedia.org/wiki/C_preprocessor#Multiple_evaluation_of_side_effects, I came across this example: \#define max(a,b) \ ({ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a > _b ? _a : _b; }) // WHY DOES THIS LINE WORK? Which you can use exactly like a function, i.e. max(1,2) is an expression evaluating to 2. My QUESTION is, How does ({ statment-list last-expression; }) construct evaluate to the value of last-expression? Specifically, what does a parse tree of this

How to output preprocessed code AND compile it (Visual Studio)

只愿长相守 提交于 2019-12-09 17:03:10
问题 I'm generating preprocessor output (.i) from Visual Studio, but also want to do the actual build. Is there a combination of flags that will both output the .i file without then stopping the compiler from going ahead with the build as normal? This is currently just C++ but will probably want to use this with CUDA later, so prefer answers that work within Visual Studio rather than require command line (unless it works for CUDA too). The point of this is to save the time it takes to do Project-

Best practice on writing constant parameters for embedded systems

痴心易碎 提交于 2019-12-09 16:00:19
问题 This is a case of "static const” vs “#define” in C" for embedded systems. On large/mid projects with "passed-down" code and modules, what is the best practice on writing constant parameters for your include files, modules, etc? In a code "passed-down" where you don't know if the names you're choosing are defined in some other included file or might be called with extern or as macros in some other file that might include your file. Having these 3 options: static const int char_height = 12;

Type-generic programming with macros: tricks to determine type?

僤鯓⒐⒋嵵緔 提交于 2019-12-09 15:58:57
问题 It's possible to do certain types of type-generic-functions as macros in C , for instance things like: #define SQRT(x) (sizeof(x) == sizeof(float) ? sqrtf((x)) : \ sizeof(x) == sizeof(double) ? sqrt((x)) : \ sqrtl((x)) ) This works (mostly) as expected as long as x is a floating point type. But what if I want a type-generic macro that can take either an integer type or a pointer type, which might have the same size. Is there a clever way to test whether the macro argument is an integer or a

Is there a way to have the C Preprocessor resolve macros in an #error statement?

Deadly 提交于 2019-12-09 15:05:07
问题 Just as the title says. I want to use a preprocessor macro in the text of an #error statement: #define SOME_MACRO 1 #if SOME_MACRO != 0 #error "SOME_MACRO was not 0; it was [value of SOME_MACRO]" #endif In this example I want the preprocessor to resolve [value of SOME_MACRO] to the actual value of SOME_MACRO which in this case is 1. This should happen before the preprocessor, compiler or whatever processes #error prints the error output Is there a way to do that or is this just not possible?

Passing an initialization list to a macro

给你一囗甜甜゛ 提交于 2019-12-09 14:36:36
问题 Why doesn't the commented out line in the following program compile? #include <iostream> #include <vector> using namespace std; #define F1(a) 1 int F2(vector<int>) { return 2; } int main() { vector<int> v; v = vector<int>{1,2,3}; cout << F1( v ) << endl; //The following line doesn't compile. The error is: //error: macro "F" passed 3 arguments, but takes just 1 //cout << F1( vector<int>{1,2,3} ) << endl; // <- error! cout << F1( vector<int>({1,2,3}) ) << endl; cout << F1( (vector<int>{1,2,3})

“with” macro in C

早过忘川 提交于 2019-12-09 13:17:35
问题 I was looking for a macro that will resemble the with-construct. The usage should be something like: with (lock(&x), unlock(&x)) { ... } It might be useful for some other purposes. I came up with this macro: #define __with(_onenter, _onexit, v) \ for (int __with_uniq##v=1; __with_uniq##v > 0; )\ for (_onenter; __with_uniq##v > 0; _onexit) \ while (__with_uniq##v-- > 0) #define _with(x, y, z) __with(x, y, z) #define with(_onenter, _onexit) _with(_onenter, _onexit, __COUNTER__) It has 3 nested

-Werror causes compiler to stop on #warning. What can I do to prevent this?

吃可爱长大的小学妹 提交于 2019-12-09 11:13:38
问题 First off, I want it to stop on warnings. But I also want to print out some informative messages (like "Come back and implement this!"). Unfortunately, my compiler doesn't support #info , #message , #pragma message() , etc. I know there's -Wno-error=<something> , but my google-foo is weak, and I can't seem to find out the <something> for #warning . I've tried -Wno-error=warning , and that just says "there's no -Wwarning ". Same with " warn ". Any suggestions? For what its worth, I'm using the

What are the differences between the C and C++ preprocessors? [duplicate]

自作多情 提交于 2019-12-09 10:06:34
问题 This question already has answers here : Is a C++ preprocessor identical to a C preprocessor? (3 answers) Closed 5 years ago . Are there any differences in behaviour between the C and C++ preprocessors? They are defined by different passages of standards text (section 6.10 of the C standard and section 16 of the C++ standard). My motivation for asking this is that a proposal for making the single quote a digit separator that was recently accepted into C++14 extends the C++ preprocessor

Size of #define values

流过昼夜 提交于 2019-12-09 09:28:30
问题 If a value is defined as #define M_40 40 Is the size the same as a short (2 bytes) or is it as a char (1 byte) or int (4 bytes)? Is the size dependent on whether you are 32-bit or 64-bit? 回答1: #define has no size as it's not a type but a plain text substitution into your C++ code. #define is a preprocessing directive and it runs before your code even begins to be compiled . The size in C++ code after substitution is whatever the size is of what C++ expression or code you have there. For