c-preprocessor

Can C/C++ preprocessor macros have default parameter values? [duplicate]

大憨熊 提交于 2019-12-05 15:23:58
问题 This question already has answers here : Optional Parameters with C++ Macros (14 answers) Overloading Macro on Number of Arguments (8 answers) Closed 4 years ago . Can we specify default parameter values for macro parameters? I know there isn't any type-checking, so I expect the default value to be nothing more than just some text used by the preprocessor for macro expansion in instances where the parameter value is not specified. 回答1: You are looking for a macro overload mechanism which is

C preprocessor library

99封情书 提交于 2019-12-05 15:06:16
I have a task of developing source analysis tool for C programs, and I need to pre-process code before the analysis itself. I was wondering what is the best library for this. I need something light-weight and portable. NPE Instead of rolling out your own, why not use cpp that's part of the gcc suite: http://gcc.gnu.org/onlinedocs/gcc-4.6.1/cpp/ CPP(1) GNU CPP(1) NAME cpp - The C Preprocessor SYNOPSIS cpp [-Dmacro[=defn]...] [-Umacro] [-Idir...] [-iquotedir...] [-Wwarn...] [-M|-MM] [-MG] [-MF filename] [-MP] [-MQ target...] [-MT target...] [-P] [-fno-working-directory] [-x language] [-std

How to disable #line directives being written to the T4 generation output file

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 12:50:28
问题 I have encountered a small problem with my T4 code generation. I have broken my T4 templates up into separate files and placed them in various directories, I have done this so parts of my code generation may be re-used in multiple projects, e.g. model generation, repository generation and service generation all include a core EntityGeneration.tt file. Unfortunately, when TextTemplating resolves my nested includes, it builds up a long #line pre-processor directive in its generated .cs file,

#define a special operator in c++

孤人 提交于 2019-12-05 12:24:28
Say that I want to make up a special operator !+ in C++ between two objects. I would like to use !+, on example, because I think it is much more meaningful than any other operator. One basic thing I could do is to find a free, unused operator and make the replacement work with a #define: #define !+ % class myclass { public: int operator %(myclass &c) { return 3; } } So that if I later write something like a!+b with a and b instances of myclass, it would work. Now, is there any way to define it instead that with an operator, with some function? Something like: #define a!+b -> a

Difference between gcc and Microsoft preprocessor

主宰稳场 提交于 2019-12-05 10:50:29
问题 I discovered that Microsoft Visual Studio compiler and gcc preprocess the following small snippet differently: # define M3(x, y, z) x + y + z # define M2(x, y) M3(x, y) # define P(x, y) {x, y} # define M(x, y) M2(x, P(x, y)) M(a, b) 'gcc -E' gives the following: a + {a + b} , while 'cl /E' issues a warning about missing macro argument and produces the following output: a + {a, b} + It seems that commas that came from nested macro expansions are not considered to be argument separators.

Why is post-compilation code injection a better idea than pre-compilation code injection?

亡梦爱人 提交于 2019-12-05 10:39:32
So we all know that C# doesn't have a C-like macro pre-processor (and there's a good thread on why here ). But now that AOP is gaining traction, it seems like we're starting to do stuff with post-processors that we used to do with pre-processors (bear in mind that I am only getting my feet wet with PostSharp so am perhaps off base). I am a huge fan of attributes in C#, but if a pre-processor was left out for good reasons (which, as a former MFC user I still question but nevertheless accept) why is post-compilation code injection a better idea than pre-compilation code injection? The reasons

Initialize a 2D array of unknown size using macros in C

故事扮演 提交于 2019-12-05 10:23:36
I was working on a small macro project that requires me to pass a 2 dimensional array literal to one of my macros like so: myMacro({{0, 1, 2}, {2, 1, 0}}) . Without having to pass the size of the array literal to the macro, is there a way to have it expand to the following: int[2][3] = { {0, 1, 2}, {2, 1, 0} } or something equivalent (any initialization that preserves the shape of the array will work)? Thanks in advance for any help #include <boost/preprocessor/tuple/size.hpp> #include <boost/preprocessor/tuple/elem.hpp> #include <boost/preprocessor/variadic/to_seq.hpp> #include <boost

How do I create my own defined constants based on the “Configuration Manager”?

半世苍凉 提交于 2019-12-05 09:58:51
问题 When I select the "Debug" configuration, the DEBUG constant is active. When I select the "Release" configuration, the DEBUG constant is inactive. How can I create my own configurations so that they include my own defined constants. Basically, I want it so that if I select the configuration "FOOBAR" that there is a constant FOO and BAR in my project active. I'm basically trying to avoid putting in a bunch of #define FOO in my projects, then commenting/uncommenting them out when I need/don't

How to get Xcode 8 C preprocessor to ignore // comments in #defines

和自甴很熟 提交于 2019-12-05 09:58:08
The C preprocessor ( cpp ) seems like it should handle this code correctly: #define A 1 // hello there int foo[A]; I would expect to replace A with 1 . What happens is that A is replaced with 1 // hello there , which results in the following output from cpp -std=c99 test.c : # 1 "test.c" int foo[1 // hello there]; Which is not valid C and fails to compile. How can I get cpp to perform the proper replacement? Note on compiler: Using cpp from the latest (8.2.1, Dec 2016) Xcode on mac, so I doubt it's due to an outdated compiler. Somewhat to my surprise, I can reproduce the problem on my Mac