preprocessor

C++ Translation Phase Confusion

Can someone explain why the following doesn't work? int main() // Tried on several recent C++ '03 compilers. { #define FOO L const wchar_t* const foo = FOO"bar"; // Will error out with something like: "identifier 'L' is undefined." #undef FOO } I thought that preprocessing was done in an earlier translation phase than string lit...

Using C preprocessor to construct a string literal for scanf?

I'm attempting to create an sscanf string literal to aid in buffer overrun prevention in C99. The goal is something like: #define MAX_ARG_LEN 16 char arg[MAX_ARG_LEN] = ""; if (sscanf(arg, "%"(MAX_ARG_LEN-1)"X", &input) > 0) The obvious "manual" solution is something like: #define MAX_ARG_LEN 16 #define MAX_ARG_CHARS "15" char...

C++ and preprocessor macro gotcha

Can you figure out what is wrong with the statement below? GCC error states: "'type name' declared as function returning array". #define MACRO(a) (a)[1] class index { public: typedef int index_type[2]; const index_type& operator[](int i) const; }; int k = 0; int i = MACRO(index()[k]); btw: I know what is wrong, I thought i...

How, exactly, does the double-stringize trick work?

At least some C preprocessors let you stringize the value of a macro, rather than its name, by passing it through one function-like macro to another that stringizes it: #define STR1(x) #x #define STR2(x) STR1(x) #define THE_ANSWER 42 #define THE_ANSWER_STR STR2(THE_ANSWER) /* "42" */ Example use cases here. This does work, at least i...

Preprocessor not skipping asm directives

I'm programming to a microprocessor (Coldfire) and I don't have access every day to the microprocessor trainer so I want to be able to execute some of my code on the computer. So I'm trying to skip a part of my code when executing on the computer by defining _TEST_. It doesn't work. It tries to compile the asm code and dies whinin...

Change case of argument provided to C preprocessor macro

I'm fairly new to the C preprocessor. Is it possible to change the case of an argument provided to a function-like #define? For example, I want to write test(size) and then involve "Size" in the resulting replacement. ...

What are the lengths/limits C preprocessor as a language creation tool? Where can I learn more about these?

In his FAQ @ http://www2.research.att.com/~bs/bs_faq.html#bootstrapping, Bjarne Stroustrup says: To build [Cfront, the first C++ compiler], I first used C to write a "C with Classes"-to-C preprocessor. "C with Classes" was a C dialect that became the immediate ancestor to C++... I then wrote the first version of Cfront i...

Separate specific #ifdef branches

In short: I want to generate two different source trees from the current one, based only on one preprocessor macro being defined and another being undefined, with no other changes to the source. If you are interested, here is my story... In the beginning, my code was clean. Then we made a new product, and yea, it was better. But the co...

How to prevent duplicates, macro or something?

Well, the problem is that I've got a lot of code like this for each event passed to the GUI, how can I shortify this? Macros wont do the work I guess. Is there a more generic way to do something like a 'template' ? private delegate void DownloadProgressDelegate(object sender, DownloaderProgressArgs e); void DownloadProgress(object sende...

What does #error do?

I am guessing from # that it is only a compile-time utility. How can it be used in C/C++ programs? Did not find much about it on the internet. Any links would be helpful. ...

What is the purpose of the #define directive in C++?

What is the role of the #define directive? ...

#ifdef with gfortran 77

I am using gfortran to compile FORTRAN 77 and would like to have DEBUG build options by using the preprocessor directive #ifdef. However, when I use them I get compile time warnings "Illegal preprocessor directive". Is it possible to have this functionality without deviating from the standard toolchain? ...

Preprocessor directive #ifndef for C/C++ code

In eclipse, whenever I create a new C++ class, or C header file, I get the following type of structure. Say I create header file example.h, I get this: /*Comments*/ #ifndef EXAMPLE_H_ #define EXAMPLE_H_ /* Place to put all of my definitions etc. */ #endif I think ifndef is saying that if EXAMPLE_H_ isn't defined, define it, which may...

preprocessor macros i don't understand

i'm currently looking through some source code i found on the net, which makes use of preprocessor macros in a way i don't understand. it implements the quad-edge data-structure. hope, someone can clear things for me! typedef int edge_ref; typedef struct { edge_ref next[4]; void *data[4]; unsigned mark; } edge_struct; #def...

Synchronizing indices of function pointer table to table contents

In the embedded system I'm working on, we are using a table of function pointers to support proprietary Dynamic Libraries. We have a header file that uses named constants (#define) for the function pointer indices. These values are used in calculating the location in the table of the function's address. Example: *(export_table.c)...

C, Objective-C preprocessor output

Is there a way to get pre-processed C/Objective-C code? I have some files I acquired and would like to see the code produced by some #defines. ...

C++ preprocessing error in the code

#include "iostream" #include "string" using namespace std; #define AA(bb) \ string(::##bb); int main (int argc, char *argv[]) { AA(aa); } This gives me a bunch of errors but I am trying to understand this error pre.cpp:11:1: error: pasting "::" and "aa" does not give a valid prepr...

Compile-time (preprocessor) hashing of string

Is there any way to create a hash of string at compile time using the C/C++ preprocessor (or even template-metaprogramming)? e.g. UNIQUE_SALT("HelloWord", 3DES); The idea is that HelloWorld will not be present in the compiled binary, just a hash. Edit: There are many of these declarations spread over a large codebase. ...

C preprocessor: using #if inside #define?

I want to write a macro that spits out code based on the boolean value of its parameter. So say DEF_CONST(true) should be expanded into "const", and DEF_CONST(false) should be expanded into nothing. Clearly the following doesn't work because we can't use another preprocessor inside #defines: #define DEF_CONST(b_const) \ #if (b_const) \...

Possible to add an EventListener to a function for Actionscript 3?

I'm trying to setup something like Aspect Oriented Programming in Actionscript 3, basically the only thing I need to be able to do is something like this: SomeClass.getMethod("methodName").addEventListener(afterMethodExecuted, function() { //run code }); This way I can run code after (or before) any method in any class has run, al...