c-preprocessor

C-preprocessor recursive macro

点点圈 提交于 2019-12-02 22:39:00
#define PP_ARG0_(arg0, ...) arg0 #define PP_REST_(arg0, ...) __VA_ARGS__ #define PP_ARG0(args) PP_ARG0_ args #define PP_REST(args) PP_REST_ args #define FUNCTION(name) void name(); #define FUNCTION_TABLE(...) \ FUNCTION(PP_ARG0((__VA_ARGS__))) \ FUNCTION_TABLE(PP_REST((__VA_ARGS__))) \ test code: FUNCTION_TABLE(f1, f2,f3,testA,testB,testC); Obviously, because of recursive expansion it will only declare void f1(); and the rest won't be expanded: void f1(); FUNCTION_TABLE(f2,f3,testA,testB,testC); What kind of trick can I use to achieve recursive expansion in this case? The problem is that I

How does #error in C/C++ work?

主宰稳场 提交于 2019-12-02 22:27:44
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. It causes the compiler (or preprocessor) to output the error message. In C++, it also renders the translation unit ill-formed (i.e., it causes compilation to fail). If you have several macros that could be defined and you want to be sure that only certain combinations of them are defined, you can use #error to cause compilation to fail if an invalid combination is defined. It can also be useful if you want to be sure that

How to make preprocessor generate a string for __LINE__ keyword?

て烟熏妆下的殇ゞ 提交于 2019-12-02 21:10:09
__FILE__ is replaced with "MyFile.cpp" by C++ preprocessor. I want __LINE__ to be replaced with "256" string not with 256 integer. Without using my own written functions like toString(__LINE__); Is that possible? How can I do it? VS 2008 EDIT I'd like to automatically Find and Replace all throw; statements with throw std::runtime_error(std::string("exception at ") + __FILE__ + " "+__LINE__); in my sources. If I use macro or function to convert __LINE__ into a string I'll need to modify each source file manually. You need the double expansion trick: #define S(x) #x #define S_(x) S(x) #define S_

How to verify a type in a C macro

ぐ巨炮叔叔 提交于 2019-12-02 21:04:01
I have been thinking about ways to validate types in C macros and so far the best way that I have come up with is this: #define ASSERT_PTYPE(TYPE, VALUE) (0 && (*(int (*)(TYPE*))0)(VALUE)) This obviously expects a type name and a pointer to that type. A similar ASSERT_TYPE macro can be made as well. This seems to work quite well with GCC. It even gives a very helpful error message in the case that the types do not match. The problems are that I am not completely certain that this is valid C or the best way for that matter. As I understand it the standard says that you can cast a function

Why should one bother with preprocessor directives?

梦想的初衷 提交于 2019-12-02 20:32:01
This question may seem rather basic, but coming from an engineering (non computer-science) background, I was unsure about what the snippets of ' # 's were in some C++ code. A quick search led me to the concise, well-explained cplusplus tutorial page on preprocessor directives. But why bother with the concept of preprocessor directives at all? Is it not possible to write equivalent code that can assign values to constants, define subroutines/function/macros and handle errors? I guess I ultimately want to know when it is good practice to use such preprocessor directives, and when it is not. You

What C preprocessor conditional should I use for OS X specific code?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 20:27:10
What C preprocessor conditional should I use for OS X specific code? I need to include a specific library if I am compiling for OS X or a different header if I am compiling for Linux. I know there is __APPLE__ but I don't know if that is a current conditional for OS X 10.x. This list of operating system macros says the presence of both __APPLE__ and __MACH__ indicate OSX. Also confirmed at line 18 of part of the source for fdisk . __APPLE__ will tell you you're compiling on an Apple platform. Unless you need to support MacOS versions before OS X, that should be good enough. Alternately, you

Can you #define a comment in C?

泪湿孤枕 提交于 2019-12-02 20:17:55
I'm trying to do a debug system but it seems not to work. What I wanted to accomplish is something like this: #ifndef DEBUG #define printd // #else #define printd printf #endif Is there a way to do that? I have lots of debug messages and I won't like to do: if (DEBUG) printf(...) code if (DEBUG) printf(...) ... No, you can't. Comments are removed from the code before any processing of preprocessing directives begin. For this reason you can't include comment into a macro. Also, any attempts to "form" a comment later by using any macro trickery are not guaranteed to work. The compiler is not

“#ifdef” inside a macro [duplicate]

你。 提交于 2019-12-02 19:56:42
This question already has answers here : #ifdef in C# (3 answers) #ifdef inside #define (6 answers) Possible Duplicate: #ifdef inside #define How do I use the character "#" successfully inside a Macro? It screams when I do something like that: #define DO(WHAT) \ #ifdef DEBUG \ MyObj->WHAT() \ #endif \ Roger Lipscombe You can't do that. You have to do something like this: #ifdef DEBUG #define DO(WHAT) MyObj->WHAT() #else #define DO(WHAT) do { } while(0) #endif The do { } while(0) avoids empty statements. See this question , for example. It screams because you can't do that. I suggest the

How can I use the compile time constant __LINE__ in a string?

若如初见. 提交于 2019-12-02 19:19:51
I can use __LINE__ as a method parameter just fine, but I would like an easy way to use it in a function that uses strings. For instance say I have this: 11 string myTest() 12 { 13 if(!testCondition) 14 return logError("testcondition failed"); 15 } And I want the result of the function to be: "myTest line 14: testcondition failed" How can I write logError? Does it have to be some monstrosity of a macro? Why do you even need it as a string? What's wrong with an integer? Here are two ways you could write logError() : #define logError(str) fprintf(stderr, "%s line %d: %s\n", __FILE__, __LINE__,

How do I show the value of a #define at compile time in gcc

[亡魂溺海] 提交于 2019-12-02 19:14:11
So far I've got as far as: #define ADEFINE "23" #pragma message ("ADEFINE" ADEFINE) Which works, but what if ADEFINE isn't a string? #define ADEFINE 23 #pragma message ("ADEFINE" ADEFINE) causes: warning: malformed ‘#pragma message’, ignored Ideally I'd like to be able to deal with any value, including undefined. To display macros which aren't strings, stringify the macro : #define STRINGIFY(s) XSTRINGIFY(s) #define XSTRINGIFY(s) #s #define ADEFINE 23 #pragma message ("ADEFINE=" STRINGIFY(ADEFINE)) If you have/want boost , you can use boost stringize to do it for you: #include <boost