preprocessor

What is the worst real-world macros/pre-processor abuse you've ever come across?

女生的网名这么多〃 提交于 2019-11-26 11:27:12
What is the worst real-world macros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)? Please add a short snippet or story if it is really entertaining. The goal is to teach something instead of always telling people "never use macros". p.s.: I've used macros before... but usually I get rid of them eventually when I have a "real" solution (even if the real solution is inlined so it becomes similar to a macro). Bonus: Give an example where the macro was really was better than a not-macro solution. Related question: When are C++ macros beneficial? From memory

Swift: how to use PREPROCESSOR Flags (like `#if DEBUG`) to implement API keys?

有些话、适合烂在心里 提交于 2019-11-26 08:05:36
问题 In Objective-C it was sometimes useful to use static string constants to define alternate API keys (for example to differentiate between RELEASE and DEBUG keys for analytics packages, like MixPanel, Flurry or Crashlytics): #if DEBUG static NSString *const API_KEY = @\"KEY_A\"; #else static NSString *const API_KEY = @\"KEY_B\"; #endif and then... [Analytics startSession:API_KEY]; How does this translate to Swift, since the Swift compiler no longer uses a preprocessor? 回答1: Apple included full

Why include guards?

有些话、适合烂在心里 提交于 2019-11-26 04:56:17
问题 Include guards, as defined here, are used to prevent loading the same code twice at compilation. Why can\'t my compiler (GCC) detect that it is loading the same code twice and have a sensible default behaviour? 回答1: Simply because you might have wanted the compiler to load that file twice. Remember, that #include simply loads a file and puts its contents in the place of the directive. This file might be a header file, but may be useful and frequently used piece of source code as well. Most

Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?

南笙酒味 提交于 2019-11-26 04:32:34
Original Question What I'd like is not a standard C pre-processor, but a variation on it which would accept from somewhere - probably the command line via -DNAME1 and -UNAME2 options - a specification of which macros are defined, and would then eliminate dead code. It may be easier to understand what I'm after with some examples: #ifdef NAME1 #define ALBUQUERQUE "ambidextrous" #else #define PHANTASMAGORIA "ghostly" #endif If the command were run with '-DNAME1', the output would be: #define ALBUQUERQUE "ambidextrous" If the command were run with '-UNAME1', the output would be: #define

Xcode / iOS: How to determine whether code is running in DEBUG / RELEASE build?

橙三吉。 提交于 2019-11-26 04:05:32
问题 I am making an app that processes sensitive credit card data. If my code is running in debug mode I want to log this data to the console and make some file dumps. However on the final appstore version (ie when it is running in release mode) it is essential all of this is disabled (security hazard)! I will try to answer my question as best I can; so the question becomes \'Is this solution path the right or best way to do it?\' // add `IS_DEBUG=1` to your debug build preprocessor settings #if(

#define in Java

坚强是说给别人听的谎言 提交于 2019-11-26 03:56:28
问题 I\'m beginning to program in Java and I\'m wondering if the equivalent to the C++ #define exists. A quick search of google says that it doesn\'t, but could anyone tell me if something similar exists in Java? I\'m trying to make my code more readable. Instead of myArray[0] I want to be able to write myArray[PROTEINS] for example. 回答1: No, because there's no precompiler. However, in your case you could achieve the same thing as follows: class MyClass { private static final int PROTEINS = 0; ...

#if DEBUG vs. Conditional(“DEBUG”)

十年热恋 提交于 2019-11-26 03:19:11
问题 Which is better to use, and why, on a large project: #if DEBUG public void SetPrivateValue(int value) { ... } #endif or [System.Diagnostics.Conditional(\"DEBUG\")] public void SetPrivateValue(int value) { ... } 回答1: It really depends on what you're going for: #if DEBUG : The code in here won't even reach the IL on release. [Conditional("DEBUG")] : This code will reach the IL, however calls to the method will be omitted unless DEBUG is set when the caller is compiled. Personally I use both

Razor view engine, how to enter preprocessor(#if debug)

混江龙づ霸主 提交于 2019-11-26 02:47:56
问题 I am writing my first razor page today, can\'t figure out how to enter #if debug #else #endif How can i enter preprocessor in razor? 回答1: I just created an extension method: public static bool IsDebug(this HtmlHelper htmlHelper) { #if DEBUG return true; #else return false; #endif } Then used it in my views like so: <section id="sidebar"> @Html.Partial("_Connect") @if (!Html.IsDebug()) { @Html.Partial("_Ads") } <hr /> @RenderSection("Sidebar", required: false) </section> Since the helper is

Can gcc output C code after preprocessing?

醉酒当歌 提交于 2019-11-26 02:32:56
问题 I\'m using an open source library which seems to have lots of preprocessing directives to support many languages other than C. So that I can study what the library is doing I\'d like to see the C code that I\'m compiling after preprocessing, more like what I\'d write. Can gcc (or any other tool commonly available in Linux) read this library but output C code that has the preprocessing converted to whatever and is also readable by a human? 回答1: Yes. Pass gcc the -E option. This will output

What is the worst real-world macros/pre-processor abuse you&#39;ve ever come across?

让人想犯罪 __ 提交于 2019-11-26 02:26:01
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. What is the worst real-world macros/pre-processor abuse you\'ve ever come across (please no contrived IOCCC answers *haha*)? Please add a short snippet or story if it is really entertaining. The goal is to teach something instead of always telling people \"never use macros\". p.s.: I\'ve used macros before... but