c-preprocessor

Detect compiler with #ifdef

一个人想着一个人 提交于 2019-12-17 19:59:10
问题 I'm trying to build a small code that works across multiple platforms and compilers. I use assertions, most of which can be turned off, but when compiling with PGI's pgicpp using -mp for OpenMP support, it automatically uses the --no_exceptions option: everywhere in my code with a "throw" statement generates a fatal compiler error. ("support for exception handling is disabled") Is there a define d macro I can test to hide the throw statements on PGI? I usually work with gcc, which has GCC

Why would somebody use an #if 1 C preprocessor directive?

試著忘記壹切 提交于 2019-12-17 19:32:58
问题 I am looking through some C source code and I don't understand the following part #if 1 typedef unsigned short PronId; typedef unsigned short LMId; # define LM_NGRAM_INT #else typedef unsigned int LMId; typedef unsigned int PronId; # undef LM_NGRAM_INT #endif Why would someone do #if 1 ? Isn't it true that only the first block will ever be processed? 回答1: Yes.. Only the first block will be processed --- until someone changes the 1 to a 0. Then the other block will be compiled. This is a

C preprocessor macro for returning a string repeated a certain number of times

人走茶凉 提交于 2019-12-17 19:19:10
问题 Does someone know of any C99 preprocessor magic that allows for creating a string consisting of another string repeated N times? E.g. STRREP( "%s ", 3 ) becomes "%s %s %s " after preprocessing. The only thing I could think of myself was something like this #define STRREP( str, N ) STRREP_##N( str ) #define STRREP_0(str) "" #define STRREP_1(str) str #define STRREP_2(str) str str #define STRREP_3(str) str str str ... which works well, but is ugly as I have to define a macro for each repetition

How can I generate a list via the C preprocessor (cpp)?

ぐ巨炮叔叔 提交于 2019-12-17 18:29:48
问题 I would like to do something like the following: F_BEGIN F(f1) {some code} F(f2) {some code} ... F(fn) {some code} F_END and have it generate the following int f1() {some code} int f2() {some code} ... int fn() {some code} int (*function_table)(void)[] = { f1, f2, ..., fn }; The functions themselves are easy. What I can't seem to do is to keep track of all of the names until the end for the function_table. I looked at this question and this question but I couldn't get anything to work for me.

Is it possible for C preprocessor macros to contain preprocessor directives?

穿精又带淫゛_ 提交于 2019-12-17 18:12:50
问题 I would like to do the equivalent of the following: #define print_max(TYPE) \ # ifdef TYPE##_MAX \ printf("%lld\n", TYPE##_MAX); \ # endif print_max(INT); Now the #ifdef or any nested preprocessor directive is not allowed as far as I can see in a function macro. Any ideas? Update: So it seems like this is not possible. Even a hack to check at runtime seems unachievable. So I think I'll go with something like: #ifndef BLAH_MAX # define BLAH_MAX 0 #endif # etc... for each type I'm interested in

Is there a preprocessor directive for detecting C++11x support? [duplicate]

末鹿安然 提交于 2019-12-17 17:59:42
问题 This question already has answers here : How do I check for C++11 support? (10 answers) Closed 5 years ago . If have some code where I would like to use C++11x extensions as much as possible, but have a fallback if this is not supported. Currently the OSX version of GCC and the VisualC compiler has little to no support for C++11x, so I use: #if (defined(__APPLE__) || (defined(_WIN32))) ...fallback code without C++11x ... #else ... code using C++11x ... #endif And this works, but is not really

How can I make the preprocessor insert linebreaks into the macro expansion result? [duplicate]

梦想的初衷 提交于 2019-12-17 16:46:20
问题 This question already has answers here : How to generate a newline in a cpp macro? (7 answers) Closed 4 years ago . With C/C++ macros it's quite easy to generated long constructs automatically. For example, if I want a huge set of methods to not ever throw exceptions (a must for COM-exposed methods) I can do something like this: #define BEGIN_COM_METHOD\ try{ #define END_COM_METHOD\ return S_OK;\ } catch( exception& ) {\ // set IErrorInfo here\ return E_FAIL;\ } to make such macros manageable

What does ## in a #define mean?

谁都会走 提交于 2019-12-17 16:26:14
问题 What does this line mean? Especially, what does ## mean? #define ANALYZE(variable, flag) ((Something.##variable) & (flag)) Edit: A little bit confused still. What will the result be without ## ? 回答1: A little bit confused still. What will the result be without ##? Usually you won't notice any difference. But there is a difference. Suppose that Something is of type: struct X { int x; }; X Something; And look at: int X::*p = &X::x; ANALYZE(x, flag) ANALYZE(*p, flag) Without token concatenation

Use #ifdefs and #define to optionally turn a function call into a comment

这一生的挚爱 提交于 2019-12-17 15:56:24
问题 Is it possible to do something like this #ifdef SOMETHING #define foo // #else #define foo MyFunction #endif The idea is that if SOMETHING is defined, then calls to foo(...) become comments (or something that doesn't get evaluated or compiled), otherwise it becomes a call to MyFunction. I've seen __noop used, but I don't believe I can use that. EDIT(s): I don't think I can really use a macro here, because MyFunction takes a variable number of arguments. Also, I'd like to make it so the

Strange behaviour of macros C/C++

ⅰ亾dé卋堺 提交于 2019-12-17 14:58:26
问题 I'm using some macros, and observing some strange behaviour. I've defined PI as a constant, and then used it in macros to convert degrees to radians and radians to degrees. Degrees to radians works fine, but radians to degrees does not: piTest.cpp: #include <cmath> #include <iostream> using namespace std; #define PI atan(1) * 4 #define radians(deg) deg * PI / 180 #define degrees(rad) rad * 180 / PI int main() { cout << "pi: " << PI << endl; cout << "PI, in degrees: " << degrees(PI) << endl;