c-preprocessor

What's the difference in practice between inline and #define?

ε祈祈猫儿з 提交于 2019-12-04 03:09:38
As the title says; what's the difference in practice between the inline keyword and the #define preprocessor directive? #define is a preprocessor tool and has macro semantics. Consider this, if max(a,b) is a macro defined as #define max(a,b) ((a)>(b)?(a):(b)) : Ex 1: val = max(100, GetBloodSample(BS_LDL)) would spill extra innocent blood, because the function will actually be called twice. This might mean significant performance difference for real applications. Ex 2: val = max(3, schroedingerCat.GetNumPaws()) This demonstrates a serious difference in program logic , because this can

Why is this nested macro replacement failing?

我怕爱的太早我们不能终老 提交于 2019-12-04 03:07:23
I am trying to apply the X Macro concept, in order to have the possibility to initialize all struct members to a custom default (invalid) value. I write the following code: #define LIST_OF_STRUCT_MEMBERS_foo \ X(a) \ X(b) \ X(c) #define X(name) int name; struct foo { LIST_OF_STRUCT_MEMBERS_foo }; #undef X #define X(name) -1, static inline void foo_invalidate(struct foo* in) { *in = (struct foo){ LIST_OF_STRUCT_MEMBERS_foo }; } #undef X #define X(name) -1, #define foo_DEFAULT_VALUE { LIST_OF_STRUCT_MEMBERS_foo } #undef X static struct foo test = foo_DEFAULT_VALUE; However, when I run the

How to see macro expansions step-by-step?

早过忘川 提交于 2019-12-04 03:04:05
问题 It seems Eclipse allows user to "see the expansion Step-by-Step" by pressing F2. I like this awesome feature. But can I do the same thing with just gcc or clang (or any tool)? -E option makes all macros fully expanded. So I haven't found any alternative way to expand macros step-by-step. Eclipse is big. I hope I don't need to install it everywhere and have it launched all the time. 回答1: It's a feature built into Eclipse. If such a tool was provided as part of the GCC or Clang toolchain,

Can __FILE__ and __LINE__ be made linkable when printed to Qt Creator's debug console?

自古美人都是妖i 提交于 2019-12-04 02:53:45
Header: #define TRACE_ERROR(s) \ { ... char TraceBuffer[512]; sprintf(TraceBuffer, "%s\t(%s:%d)", s, __FILE__, __LINE__); DebugErrTrace(TraceBuffer); ... } Implementation: void DebugErrTrace(char *String, ...) { ... qDebug() << String; } The above spits out a line of debug trace, which might look something like ERROR File Missing! (..\trunk\Common\FileManager.cpp:102) in Qt Creator's debug console. I've noticed that Qt's own error messages e.g. Object::connect: No such slot cClass::Method(QString) in ..\trunk\Components\Class.cpp:301 create what looks like a hyperlink around the __FILE__:_

Error when defining a stringising macro with __VA_ARGS__

删除回忆录丶 提交于 2019-12-04 02:17:59
I have been trying to implement a function macro in C that prepends "DEBUG: ", to the argument, and passes its arguments to printf: #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) This gives me this error in gcc: src/include/debug.h:4:70: error: expected expression before ‘)’ token #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) ^ Supposedly, it should stringise format, and pass its variable arguments to printf, but so far I can't get past this error. EDIT After giving up on stringising arguments, and double-hashing ( ## ) __VA_ARGS__ I now have this

preprocessor directives inside define? [duplicate]

早过忘川 提交于 2019-12-04 01:59:01
Possible Duplicate: C preprocessor: using #if inside #define? Is there any trick to have preprocessor directives inside rhs of define ? The problem is, preprocessor folds all rhs into one long line. But maybe there is a trick ? Example of what I'd want in the rhs is #define MY_CHECK \ #ifndef MY_DEF \ # error MY_DEF not defined \ #endif ? The purpose is a shortness: to have 1-line shortcut instead of multiline sequence of checks. As others have noted, preprocessor macros cannot expand into any other preprocessor directives; if they do you'll generally get odd errors about stray '#' characters

Why do people use #ifdef for feature flag tests?

做~自己de王妃 提交于 2019-12-04 01:36:13
People recommend #ifdef for conditional compilation by a wide margin . A search for #ifdef substantiates that its use is pervasive. Yet #ifdef NAME (or equivalently #if defined(NAME) and related #ifndef NAME (and #if !defined(NAME) ) have a severe flaw: header.h #ifndef IS_SPECIAL #error You're not special enough #endif source.cpp #include "header.h" gcc -DIS_SPECIAL source.cpp will pass, obviously, as will source1.cpp #define IS_SPECIAL 1 #include "header.h" But, so will source0.cpp #define IS_SPECIAL 0 #include "header.h" which is quite the wrong thing to do. And some C++ compilers, passed a

What does #line mean?

落爺英雄遲暮 提交于 2019-12-04 01:34:06
What does the following line do? #line 25 "CSSGrammar.y" And what's with the extension? According to the Standard: §16.4.3: A preprocessing directive of the form # line digit-sequence new-line causes the implementation to behave as if the following sequence of source lines begins with a source line that has a line number as specified by the digit sequence (interpreted as a decimal integer). If the digit sequence specifies zero or a number greater than 2147483647, the behavior is undefined. §16.4.4: A preprocessing directive of the form # line digit-sequence " s-char-sequenceopt" new-line sets

Is there a way to have the C Preprocessor resolve macros in an #error statement?

喜欢而已 提交于 2019-12-04 01:29:30
Just as the title says. I want to use a preprocessor macro in the text of an #error statement: #define SOME_MACRO 1 #if SOME_MACRO != 0 #error "SOME_MACRO was not 0; it was [value of SOME_MACRO]" #endif In this example I want the preprocessor to resolve [value of SOME_MACRO] to the actual value of SOME_MACRO which in this case is 1. This should happen before the preprocessor, compiler or whatever processes #error prints the error output Is there a way to do that or is this just not possible? I don't want to know if there is an ISO C++ standard way to do that, because afaik the preprocessor

Why are argument substitutions not replaced during rescanning?

非 Y 不嫁゛ 提交于 2019-12-04 01:13:01
Consider the following macro definitions and invocation: #define x x[0] #define y(arg) arg y(x) This invocation expands to x[0] (tested on Visual C++ 2010, g++ 4.1, mcpp 2.7.2, and Wave). Why? Specifically, why does it not expand to x[0][0] ? During macro replacement, A parameter in the replacement list...is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument’s preprocessing tokens are completely macro replaced (C++03 §16.3.1/1). Evaluating the macro invocation, we take the following steps: The function-like