c-preprocessor

How can I guarantee full macro expansion of a parameter before paste?

南楼画角 提交于 2019-12-05 04:47:38
I have a general macro: #define mSwitch( Root, Case ) Root##_Case_##Case #define mSpecialDisplay( what, Val ) mSwitch(mSpecialDisplay,what)(Val) #define mSpecialDisplay_Case_Int(Val) ...do stuff #define mSpecialDisplay_Case_Float(Val) ...do stuff ...more special cases how do I guarantee that the variable Case is fully expanded before it gets pasted in mSwitch ? It works fine if mSwitch is passed a literal value, but if there are several layers of indirection, or intermediary operations, mSwitch ends up pasting one of those before they get fully expanded. I'm using MSVC 2005 . Is there a simple

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

别等时光非礼了梦想. 提交于 2019-12-05 04:38:48
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? 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. No. Macros are expanded at the source line where they occur. You probably can... but definitely not with the restriction you mentioned (no macros). 来源: https:/

How can I use gcc's -I command to add recursive folders

十年热恋 提交于 2019-12-05 04:28:11
Is there a way to use gcc's -I command and add all the paths to search path by giving a root directory? I'm trying to use :!gcc -E myfile.c to view macro expansions, but myfile.c includes a whole bunch of other header files in different directories, and because I'm executing this command in vim, so I don't want to call a makefile, is there anyway to do this? If you are using Apple's GCC (or Clang), then you can use the following approach (which appears to be an extension): the parameter's suffix will need /** -IMON_DIRECTORY/** Now everything under MON_DIRECTORY/ may be searched. Obviously,

Is there a list of preprocessor defines for various operating systems (and versions)?

半腔热情 提交于 2019-12-05 02:55:54
问题 e.g. a mapping for Mac OS 10.6.3 aka Snow Leopard => __APPLE__ && __LP64__ ? Windows 7, Windows XP => __WIN32__ Linux => __LINUX__ 回答1: Here you go: http://predef.sourceforge.net/ 来源: https://stackoverflow.com/questions/2990172/is-there-a-list-of-preprocessor-defines-for-various-operating-systems-and-versi

Using C Preprocessing to get integer value of a string

牧云@^-^@ 提交于 2019-12-05 02:55:40
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("mov") }; Where INSN_TO_ENUM expands to the same code. The performance would be the same, but the

Why is it not advised to define macros in header files?

一曲冷凌霜 提交于 2019-12-05 02:19:07
问题 The Google C++ Style Guide guide advises that macros must not be defined in a .h (header) file. What are the cons of doing it? 回答1: The preprocessor concatenates all included source files together in order. If you don't undefine a macro, it can apply to any source following where it was first defined. Since headers are often the public API of a library, any macros you define in your headers could end up in someone else's code, doing unexpected things. Since unexpected things are the

About ## preprocessor in C

淺唱寂寞╮ 提交于 2019-12-05 01:59:45
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? Jay Sullivan 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 = cat(cat(1,2),3); What appears to be happening is that the compiler wraps the result of cat(1

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

一世执手 提交于 2019-12-05 01:59:27
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. Luchian Grigore 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 and include it use your compiler's options do it the sane way - extern const int A = 1; in an

Conditional #include in C

天大地大妈咪最大 提交于 2019-12-05 01:57:46
问题 Is there a way to make a conditional include with the c preprocessor? I have a "library" of tools (Tools.c, Tools.h), shared by different teams. This library depends on a second one, providing XML-capabilities. Lets call that one XML.h Now one team uses a plain version of the second library (XML.h), while another team uses an extended version (XMLEx.h) in their project. The second team doesn't want to include the XML.h because they already have XMLEx.h included, providing all functions of XML

Checking the sizeof an integer type in the preprocessor

笑着哭i 提交于 2019-12-05 01:11:01
How can I check the size of an unsigned in the preprocessor under g++? sizeof is out of the question since it is not defined when during preprocessing. This may not be the most elegant method, but one thing that you may be able to leverage is UINT_MAX defined in "limits.h". That is, ... if UINT_MAX == 65535, then you would know that sizeof (unsigned) = 2 if UINT_MAX == 4294967295, then you would know that sizeof (unsigned) = 4. and so on. As I said, not elegant, but it should provide some level of usability. Hope this helps. Based on Sparky's answer, here is a way that would look a bit nicer