c-preprocessor

Comma in C/C++ macro

心已入冬 提交于 2019-12-11 03:03:53
问题 Say we have a macro like this #define FOO(type,name) type name Which we could use like FOO(int, int_var); But not always as simply as that: FOO(std::map<int, int>, map_var); // error: macro "FOO" passed 3 arguments, but takes just 2 Of course we could do: typedef std::map<int, int> map_int_int_t; FOO(map_int_int_t, map_var); // OK which is not very ergonomic. Plus type incompatibilities have to be dealt with. Any idea how to resolve this with macro ? 回答1: Because angle brackets can also

ctags chokes on source file with unbalanced braces due to #ifdef

你。 提交于 2019-12-11 02:44:58
问题 I am using ctags to generate a tags file for a C project I am working on, but many functions are missing in the file. This appears to be caused by unbalanced braces in the source files due to using #ifdef . A (simplified) example: #include <stdio.h> struct mystruct { long member; #ifndef _MSC_VER }__attribute__ ((packed)); #else /* _MSC_VER */ }; #pragma pack(pop) #endif /* _MSC_VER */ char* greeting_text(){ return "Hello world\n"; } int main( int argc, const char* argv[] ){ char * greeting =

gcc -E does not expand C11 _Generic expressions

心不动则不痛 提交于 2019-12-11 02:28:23
问题 In a C11 library project I have a couple of macro functions that are exposed under a shared macro name using generics, like this: #define signum(operand) _Generic( (operand), \ unsigned long long: __signum_i4, unsigned long: __signum_i3, unsigned int: __signum_i2, unsigned short: __signum_i1, unsigned char: __signum_i0, \ signed long long: __signum_i4, signed long: __signum_i3, signed int: __signum_i2, signed short: __signum_i1, signed char: __signum_i0, \ long double: __signum_f2, double: _

How to print preprocessor macros under Sun Studio?

孤街浪徒 提交于 2019-12-11 02:25:46
问题 I'm working under Sun Studio 12.3 on SunOS 5.11 (Solaris 11.3). I need to see the macros that Sun Studio defines to fix a bug report taken under the suite. This is similar to Solaris and Preprocessor Macros, but the cited question uses GCC and its preprocessor; and not Sun Studio's preprocessor. I've run CC -flags but I don't see an option similar to GCC's cpp -dM or g++ -dM -E - </dev/null . CC does have a -E , but its fairly anemic and does not print any preprocessor definitions: $ echo

How to figure out what value MSVC is using for a preprocessor macro

倾然丶 夕夏残阳落幕 提交于 2019-12-11 02:11:16
问题 I'm attempting to use a /D compiler option on MSVC6 to define a string, but there's something weird about using double quotes around it. To debug this problem, it would be extremely helpful for me to be able to see what value the preprocessor is actually substituting into my code where the macro is expanded. Is there any way I can do this? I tried creating a Listing file with "assembly and source", but the source contains the original macro name and the ASM is some incomprehensible gibberish

Preprocessor-IF doesn't work

不打扰是莪最后的温柔 提交于 2019-12-11 01:28:50
问题 I'm trying to check with Preprocessor-Ifs if the Device is an iPad. If it is an iPad, I want to define something Devicespecific, but for some reason I can't check in an PP-IF if a PP-Constant is true. Maybe you got an idea? #ifdef UI_USER_INTERFACE_IDIOM #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #else #define IS_IPAD false #endif #if IS_IPAD #define WIDTH 768 #define HEIGHT 1024 #else #define WIDTH 320 #define HEIGHT 480 #endif 回答1: Preprocessor rules are,

Multi line inline assembly macro with strings

让人想犯罪 __ 提交于 2019-12-11 01:26:19
问题 I'm trying to implement a macro ( "MY_MACRO" ), which stores a string preceded by a 32 bit integer number in a certain section ("my_section") . Example: MY_MACRO(200, "my first string %u %x") ; Here are the options I tried and the problems I'm facing with. I would appreciate any help. ( gcc 4.7.3. MIPS cpu ) Option A: #define MY_MACRO(_num, _string)\ asm volatile(".pushsection .my_section");\ asm volatile(".byte %0, %1, %2, %3" : : "i"((_num >> 24) & 0xFF), "i"((_num >> 16) & 0xFF), "i"((_num

Do function like macros need a mandatory parentheses? I am confused after referring the GCC cpp manual

橙三吉。 提交于 2019-12-11 01:15:13
问题 Here is what confuses me: To define a function-like macro, you use the same '#define' directive, but you put a pair of parentheses immediately after the macro name. I believe this is to make the code stand out for people other than the author of the program. Like other rules of CAPS for macro names. But the following is where I get confused: A function-like macro is only expanded if its name appears with a pair of parentheses after it. If you write just the name, it is left alone. I disagreed

bridging templates with runtime arguments

℡╲_俬逩灬. 提交于 2019-12-11 00:17:43
问题 I am dealing with a 3rd party C++ library which makes extensive use of templates. That makes it difficult to create a C API for it to use it from my framework. Abstracting the problem, suppose the library offers the function: template <int i> void foo(); template <int i> void zoo(int v); I want to create a C API with the function head: extern "C" void c_foo(int i); extern "C" void c_zoo(int i, int v); An obvious implementation could be: void c_foo(int i) { switch(i) { case 1: foo<1>(); break;

C function decorators (wrappers) at compile time

∥☆過路亽.° 提交于 2019-12-10 23:36:37
问题 I'm trying to change the behaviour of some functions in C with help of the preprocessor; and also add optional parameters that can be set on or off... The basic pattern for the optional parameters is easy: #ifdef OPT_PARAM #define my_func(a, b, opt) _my_func(a, b, opt) #else #define my_func(a, b, opt) _my_func(a, b) #endif /*the rest of the code always calls "my_func" with all the params and not the underscored version...*/ #ifdef OPT_PARAM void _my_func(int a, int b, int opt) #else void _my