c-preprocessor

Regex for matching C++ string constant

狂风中的少年 提交于 2020-08-06 13:09:05
问题 I'm currently working on a C++ preprocessor and I need to match string constants with more than 0 letters like this "hey I'm a string . I'm currently working with this one here \"([^\\\"]+|\\.)+\" but it fails on one of my test cases. Test cases: std::cout << "hello" << " world"; std::cout << "He said: \"bananas\"" << "..."; std::cout << ""; std::cout << "\x12\23\x34"; Expected output: std::cout << String("hello") << String(" world"); std::cout << String("He said: \"bananas\"") << String("...

C Preprocessor: Own implementation for __COUNTER__

断了今生、忘了曾经 提交于 2020-07-29 06:38:26
问题 I'm currently using the __COUNTER__ macro in my C library code to generate unique integer identifiers. It works nicely, but I see two issues: It's not part of any C or C++ standard. Independent code that also uses __COUNTER__ might get confused. I thus wish to implement an equivalent to __COUNTER__ myself. Alternatives that I'm aware of, but do not want to use: __LINE__ (because multiple macros per line wouldn't get unique ids) BOOST_PP_COUNTER (because I don't want a boost dependency) BOOST

Why does the preprocessor directive in one function affect the compilation of another?

回眸只為那壹抹淺笑 提交于 2020-07-15 15:18:09
问题 Following program compiles successfully and print 1000 without even calling a foo() function from our main() function. How is it possible? #include<stdio.h> void foo() { #define ans 1000 } int main() { printf("%d", ans); return 0; } 回答1: #define is run by the preprocessor which is staged before the compiler. After the preprocessor is done, the code will look like this: /* Everything that is inside stdio.h is inserted here */ void foo() { } int main() { printf("%d", 1000); return 0; } And this

Assign two C preprocessor macros in the same command

本秂侑毒 提交于 2020-07-09 08:14:36
问题 Is it possible to assign the same value to two C preprocessor macros at the same time? It would behave similar to this normal C code: a = b = 1; I know you can do this: #define VAR1 1 #define VAR2 1 But that a bit of a pain just because there's now code replication and more opportunity to mess things up. Is the only solution this? #define VAR1 1 #define VAR2 VAR1 EDIT: As the various comments have pointed out, the preprocessor has macros, not variables. Sorry about that. 回答1: The ISO C

Assign two C preprocessor macros in the same command

若如初见. 提交于 2020-07-09 08:11:33
问题 Is it possible to assign the same value to two C preprocessor macros at the same time? It would behave similar to this normal C code: a = b = 1; I know you can do this: #define VAR1 1 #define VAR2 1 But that a bit of a pain just because there's now code replication and more opportunity to mess things up. Is the only solution this? #define VAR1 1 #define VAR2 VAR1 EDIT: As the various comments have pointed out, the preprocessor has macros, not variables. Sorry about that. 回答1: The ISO C

C preprocessor tokenization does not expand macro?

对着背影说爱祢 提交于 2020-07-02 18:05:26
问题 1) Why is the macro MSG not expanded in the following expression? #define MSG Hello #define HELLO(name) MSG ## name void HELLO(Dave) () {} Using gcc -E -P test.cpp Output: void MSGDave () {} MSG name expands to Hello Dave . And MSG # name expands to Hello "Dave" . So what causes gcc not to expand MSG ## name ? 2) Is there a workaround? Is there a preprocessor directive like defined(x), such as expand(x)? 回答1: #define MSG Hello #define cat(x, y) x ## y #define cat2(x, y) cat(x, y) #define

Replace a string in a macro variable?

雨燕双飞 提交于 2020-06-26 13:51:26
问题 I have a macro where I pass in an argument and use that define a new variable based on the name of the input: #define DO_X(x) char _do_x_var_ ## x; /* other things */ The problem is if I pass in a struct variable, it breaks: DO_X(some_struct->thing) becomes: char _do_x_var_some_struct->thing; /* other things */ EDIT: What I want it to evaluate to is: char _do_x_var_some_struct__thing; /* other things */ (or any valid variable name containing something similar to the input) What I actually

Using and returning output in C macro

。_饼干妹妹 提交于 2020-06-24 05:16:10
问题 I'm trying to instrument some code to catch and print error messages. Currently I'm using a macro somethng like this: #define my_function(x) \ switch(function(x)) { \ case ERROR: \ fprintf(stderr, "Error!\n"); \ break; \ } Normally, I never capture the function output and this works fine. But I've found a couple cases where I also need the return value of function() . I tried something like the following, but this produces a syntax error. #define my_function(x) \ do { \ int __err = function(x

Visual C++ dump preprocessor defines

筅森魡賤 提交于 2020-06-23 06:05:50
问题 I'm trying to find out all the preprocessor defines of the Visual C++ compiler (MSVC). I can do gcc -dM -E - < /dev/null on GCC to dump all the preprocessor defines. Do we have something similar with the Visual C++ compiler compiler? I'm using Visual C++ 9.0. 回答1: There is no such command. However, MSDN (both online and offline) lists all the preprocessor defines, both Microsoft specific, standard defined and ANSI defines. 来源: https://stackoverflow.com/questions/4640267/visual-c-dump

Visual C++ dump preprocessor defines

爱⌒轻易说出口 提交于 2020-06-23 06:05:11
问题 I'm trying to find out all the preprocessor defines of the Visual C++ compiler (MSVC). I can do gcc -dM -E - < /dev/null on GCC to dump all the preprocessor defines. Do we have something similar with the Visual C++ compiler compiler? I'm using Visual C++ 9.0. 回答1: There is no such command. However, MSDN (both online and offline) lists all the preprocessor defines, both Microsoft specific, standard defined and ANSI defines. 来源: https://stackoverflow.com/questions/4640267/visual-c-dump