c-preprocessor

Is there a way to create a preprocessor macro for a function?

删除回忆录丶 提交于 2019-12-24 02:03:09
问题 Is it possible to create a C++ preprocessor macro based on a function result? For example, I'd like to save the screen height dynamically in a preprocessor macro definition: #define SCREEN_HEIGHT GetSystemMetrics(SM_CYVIRTUALSCREEN) Then I want to use the result to set values based on the screen height: #if SCREEN_HEIGHT < 1200 #define TOP_COORD 200 #define BOTTOM_COORD 500 #define LEFT_COORD 0 #define RIGHT_COORD 1280 #else #define TOP_COORD 1100 #define BOTTOM_COORD 1400 #define LEFT_COORD

CPP Macro: Counter that gives number of instantiations/calls

六月ゝ 毕业季﹏ 提交于 2019-12-24 01:42:30
问题 I want to have a C preprocessor macro that knows the number of instantiations/macro calls of this macro so far. Example: int main() { printf("%d\n", MACRO()); printf("%d\n", MACRO()); } Should print 0 1 Is something like this possible? Note that it is not enough to forward this to a function as proposed below. It should work in the following context: // global variable std::vector<bool> calls_hit; #define OTHER_MACRO() \ { \ const int counter = MACRO(); \ calls_hit.resize(std::max(calls_hit

CPP Macro: Counter that gives number of instantiations/calls

拟墨画扇 提交于 2019-12-24 01:42:11
问题 I want to have a C preprocessor macro that knows the number of instantiations/macro calls of this macro so far. Example: int main() { printf("%d\n", MACRO()); printf("%d\n", MACRO()); } Should print 0 1 Is something like this possible? Note that it is not enough to forward this to a function as proposed below. It should work in the following context: // global variable std::vector<bool> calls_hit; #define OTHER_MACRO() \ { \ const int counter = MACRO(); \ calls_hit.resize(std::max(calls_hit

Can i write preprocessor directives anywhere in my c program?

穿精又带淫゛_ 提交于 2019-12-24 00:49:02
问题 Is it mandatory to write #include at the top of the program and outside the main function? I tried using #define preprocessor inside the main function and it worked fine with only one exception..that being the constant which i defined using the define directive can be used only after the line #define For instance say printf("%d",PI); #define PI 3.14 will give error "Undefined symbol PI". But in the following code i did not encounter any error #define PI 3.14 printf("%d",PI); Is this because C

Always same effect of #pragma pack(16) and #pragma pack(8)?

痴心易碎 提交于 2019-12-23 22:52:06
问题 I am trying to align data members by using #pragma pack (n). Take the following as an example: #include <iostream> using namespace std; #pragma pack(8) // or (16) struct A { int a; char b; char c; char d; char e; char f; double g; }; int main() { cout << sizeof(A) << endl; return 0; } Both will print 24 for #pragma pack(8) and #pragma pack(16) . I can understand the result for n=8 with the data alignment, of my understanding, as follows: Bytes: |1 2 3 4|5|6|7|8|9|10 11 12 13 14 15 16|17 18 19

Detect -xarch option in the preprocessor?

十年热恋 提交于 2019-12-23 22:22:56
问题 I'm using Sun Studio 12.4 and 12.5 on Solaris 11. We have a source file that provides a straight C/C++ implementation of CRC32, or an optimized version of CRC32 using Intel intrinsics. At runtime, a function pointer is populated with the proper implementation. Testing on a x86 server with dual Xeon's is producing the following because we are making code paths available based on compiler versions. SunCC 12.1 added support for SSE4 (if I parsed the matrix properly), so we attempt to enable it

Use C preprocessor to iterate over structure fields

…衆ロ難τιáo~ 提交于 2019-12-23 20:17:39
问题 I have several different C++ structs and classes with fields of the same name, that I have to copy between frequently. I would like to do something like: (in bashy pseudocode) struct S{double a; double b; double c;}; class C{public: void set_a(double a); void set_b(double b); void set_c(double c); }; S s; C c; #FOR F in FIELDSOF(S) c.set_${F}(${F}); #ENDFOR Whether or not it a good idea, is there a way to abuse either the C++ preprocessor or C++ templates to achieve this? I use g++ and clang+

Macro Meta Programming

血红的双手。 提交于 2019-12-23 19:44:50
问题 I know this is probably either bad or impossible, but since this isn't a recursive macro I think it should be possible. #define FOO 15 #define MAKE_BAR(x) BAR_##x #define MY_FOO_BAR MAKE_BAR(FOO) I'd like MY_FOO_BAR to evaluate to BAR_15. Is there a way to tell the preprocessor to evaluate FOO before passing it into MAKE_BAR? 回答1: You need another level of macro calls: #define FOO 15 #define MAKE_BAR_INNER(x) BAR_##x #define MAKE_BAR(x) MAKE_BAR_INNER(x) #define MY_FOO_BAR MAKE_BAR(FOO) This

Variadac Macro apply macro to all arguments

偶尔善良 提交于 2019-12-23 19:43:30
问题 I was experimenting with C++11 variadac macros. I was trying to apply another macro to each argument in the list. This is my first try: #define APPLY_CHAIN(first, ...) APPLY_ACT(first) APPLY_CHAIN( __VA_ARGS__ ) Unfortunately this did not work. I eventually got it to work. But it was a bit convoluted and has a limit of 'n' (where 'n' is a max size that I am willing to type macros for). #define COUNT_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define COUNT(...) COUNT_N( __VA_ARGS__,

What are the deviations between preprocessors of GCC/CLANG vs MSVC?

匆匆过客 提交于 2019-12-23 18:34:28
问题 The following preprocessor macros (the usual suspects: testing on empty argument list and counting number of arguments) run without warnings on gcc/clang but fail on Microsoft VisualC: // IS_EMPTY() returns nothing if the parameter list is empty and a single ',' (comma) otherwise. // The parameter list can have up to 32 parameters #define IS_EMPTY(...) IS_EMPTY1(__VA_ARGS__) #define IS_EMPTY1(...) IS_EMPTY2(DROP_PARAMS __VA_ARGS__ (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,EMPTY)) #define IS_EMPTY2(..