c-preprocessor

Can I default a function argument to the value of __FILE__ at the caller?

ε祈祈猫儿з 提交于 2019-12-07 01:22:59
问题 In C++, can I have a defaulted argument to a function which defaults to __PRETTY_FUNCTION___ , ___FILE___ , and ___LINE__ as defined at the point of the caller and not the point the defaults are supplied in a header file without using macros? 回答1: You can't, but you can acheive this behavior with an additional macro. For instance: #DEFINE THROW(e) throwException(e, __FILE__, __LINE__); On a side note, __PRETTY_FUNCTION__ is not standard. 回答2: No. Macros are expanded at the source line where

Understanding macros in C [duplicate]

假装没事ソ 提交于 2019-12-07 00:27:56
问题 This question already has answers here : Why these consecutive macro replacements do not result in an error? (3 answers) Closed last year . Why is the output from the following code the value 5? #include<stdio.h> #define A -B #define B -C #define C 5 int main() { printf("The value of A is %d\n", A); return 0; } 回答1: This is a tricky question because it is a stress test for the compiler preprocessor. Depending if the preprocessor is an integrated phase of the compiler or a separate program

Using C Preprocessing to get integer value of a string

假如想象 提交于 2019-12-06 21:58:01
问题 How would I create a C macro to get the integer value of a string? The specific use-case is following on from a question here. I want to change code like this: enum insn { sysenter = (uint64_t)'r' << 56 | (uint64_t)'e' << 48 | (uint64_t)'t' << 40 | (uint64_t)'n' << 32 | (uint64_t)'e' << 24 | (uint64_t)'s' << 16 | (uint64_t)'y' << 8 | (uint64_t)'s', mov = (uint64_t)'v' << 16 | (uint64_t)'o' << 8 | (uint64_t)'m' }; To this: enum insn { sysenter = INSN_TO_ENUM("sysenter"), mov = INSN_TO_ENUM(

String concatenation for include path

浪尽此生 提交于 2019-12-06 20:34:49
问题 Is there a way to concatenate 2 strings literals to form an include path? Code stub: #define INCLUDE_DIR "/include" #include INCLUDE_DIR "/dummy.h" Looking at this question, the answers point in a different direction (compiler command line). It is mentioned here that it is seemingly not possible, but I wonder if the topic has been dug enough. (I do have an use case in which this is relevant, please focus your answers/comments on this question only.) 回答1: I'm not sure that this is exactly what

Utility of macros for enum

笑着哭i 提交于 2019-12-06 20:15:56
问题 One header socket.h on my Linux system looks like the following. /* Bits in the FLAGS argument to `send', `recv', et al. */ enum { MSG_OOB = 0x01, /* Process out-of-band data. */ #define MSG_OOB MSG_OOB MSG_PEEK = 0x02, /* Peek at incoming messages. */ #define MSG_PEEK MSG_PEEK MSG_DONTROUTE = 0x04, /* Don't use local routing. */ #define MSG_DONTROUTE MSG_DONTROUTE ... Defining an enum is sort of an idiom for creating type-safe-ish constants in C that the language actually treats as compile

Is it possible to use #define from other cpp file?

╄→гoц情女王★ 提交于 2019-12-06 20:14:42
问题 I think the preprocessor handles files one by one and I can't figure out how to do it with includes, so I think it's impossible, but it would be great to hear other's thoughts. I have in a.cpp : #define A 1 and I want to use it from 2.cpp . EDIT: I cant modify first file. So for now i just have copied defines. But question still opened. 回答1: Defines inside a source file aren't seen by other translation units. Implementation files are compiled separately. You can either put them in a header

About ## preprocessor in C

孤街浪徒 提交于 2019-12-06 20:00:02
问题 Given #define cat(x,y) x##y The call cat(a,1) returns a1 , but cat(cat(1,2),3) is undefined. However if I also define #define xcat(x,y) cat(x,y) , then the result of xcat(xcat(1,2),3) is now 123 . Can anybody please explain in detail why this is so? 回答1: I tested this using both GCC and Clang. GCC gives the error: test.c:6:1: error: pasting ")" and "3" does not give a valid preprocessing token Clang gives the error: test.c:6:11: error: pasting formed ')3', an invalid preprocessing token int b

Behavior of __LINE__ in inline functions

狂风中的少年 提交于 2019-12-06 17:40:45
问题 I have a macro that passes the line number and file name to an error handler: #define SYSTEM_FAILURE (error_code, comment) \ System_Failure((error_code), (comment), __LINE__, __FILE__); How will the __LINE__ be resolved when used inside an inlined function? file.h: inline int divide(int x, int y) { if (y == 0) { SYSTEM_FAILURE(ENUM_DIVIDE_BY_ZERO, "divide by zero error"); } return x/y; } Will __LINE__ contain the line number within the header file, or the line number of the source file where

How to print C-preprocessor variables like __LINE__ with mexErrMsgTxt() In Matlab MEX

你离开我真会死。 提交于 2019-12-06 16:39:22
For debugging Matlab-MEX, which can be quite a hassle, it would be nice to have better assertion capabilities. Following this question about mex-assertions , it is possible to define a preprocessor makro, that throws an error to Matlab and prints a string (can mostly replace mxAssert , which unfortunately crashes Matlab2011b). #define myassert( isOK,astr ) ( (isOK) ? (void)0 : (void) mexErrMsgTxt(astr) ) It would be much nicer to print the file, line number and caller function, from where following example assertion myassert(A=B,"A not B") is raised! This answer to the initial question states

Multiple inclusion in multiple files

血红的双手。 提交于 2019-12-06 16:15:25
问题 I am making a small game. In BattleRecord.h: #ifndef _CHARACTER_H_ #define _CHARACTER_H_ #include "Character.h" #endif class BattleRecord { public: Character Attacker; Character Defender; Status status; int DamageDealt; int GoldEarned; int ExpGained; }; In Character.h: #ifndef _EQUIPMENT_H_ #define _EQUIPMENT_H_ #include "Equipment.h" #endif class BattleRecord; class Character { BattleRecord AttackEnemy(Character &Enemy); } In BattleRecord.h: #ifndef _CHARACTER_H_ #define _CHARACTEr_H_