c-preprocessor

Does “#pragma once” have the potential to cause errors?

时光怂恿深爱的人放手 提交于 2019-12-23 09:49:15
问题 All of my header files use include guards as well as pragma once : #pragma once #ifndef FILE_NAME_H #define FILE_NAME_H class foo { //foo interface.. }; #endif /* FILE_NAME_H */ I understand that pragma once is not standard and may not be the same across compilers, but is there any chance it will cause and error? Would it be better to somehow test if it's available first? #ifdef THIS_COMPILER_SUPPORTS_PRAGMA_ONCE #pragma once #endif #ifndef FILE_NAME_H #define FILE_NAME_H class foo { //foo

Preprocessor functions evaluated at compile time in C

為{幸葍}努か 提交于 2019-12-23 09:03:56
问题 I want to write preprocessor functions/arrays that are evaluated at compile time. For example, if I define #define MYARR[] {5,4,3,2,1,0} then, the code int x = R[0]; should be presented as int x = 5; to the compiler. (Of course only literals can be used in the index). This is important if code size/memory is critical and we don't want to store MYARR , but we need it for coding convenience. Compile time functions would also be good. For example, something like #define MYMAP(n) #if n==1 5 #else

How to use #define from another file?

…衆ロ難τιáo~ 提交于 2019-12-23 08:58:21
问题 For example: define.cs #define FOO form1.cs #if FOO MessageBox.Show("foo is set!"); #else MessageBox.Show("foo is not set!"); #endif the define.cs are included on same projet that form1.cs but the above condition give: foo is not! 回答1: You can't, but what you can do is move the define to the project configuration so that all files in the project can see the definition. See the instructions in How to define a constant globally in C# (like DEBUG). 回答2: According to this MSDN page, The scope of

Stringify first level macro expansion C

瘦欲@ 提交于 2019-12-23 07:52:39
问题 Is it possible to stringify this C macro: #define GPIO_INT_PIN (GPIO_PORT_D|GPIO_PIN_IRQ_RISING|GPIO_PIN5) using something like MY_STRINGFY(GPIO_INT_PIN) to get "(GPIO_PORT_D|GPIO_PIN_IRQ_RISING|GPIO_PIN5)" ? 回答1: Yes it is possible. Read about stringizing in GCC cpp documentation. #define STRINGIFY(It) #It #define MY_STRINGIFY(It) STRINGIFY(It) I corrected my answer thanks to Wojtek Surowka's one then use MY_STRINGIFY(GPIO_PORT_D|GPIO_PIN_IRQ_RISING|GPIO_PIN5) which would work much better if

C++ Pre-processor define after class keyword and before class name

依然范特西╮ 提交于 2019-12-23 07:30:49
问题 I recently came across this sort of code in someone's opengl shader class and am not sure of its use. As I understand it from reading IBM's documentation, the #define ONEWORD will remove any occurence of ONEWORD in the subsequent text. What is the purpose of having ONEWORD in this code at all if all occurrences are removed? What does having a token like that, after a class keyword but before a class name, really mean? I've only used #define for include guards in the past so this is entirely

is there a way to write macros with a variable argument list in visual C++?

断了今生、忘了曾经 提交于 2019-12-23 06:56:58
问题 As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is there a way to do that in VC++? 回答1: Yes but only since VC++ 2005. The syntax for your example would be: #define DBGPRINT(fmt, ...) printf(fmt, __VA_ARGS__) A full reference is here. 回答2: Yes, you can do this in Visual Studio C++ in versions 2005 and beyond (not sure about VS 2003). Take a look at VA_ARGS . You can basically do something like this: #define DBGPRINTF(fmt, ...) printf(fmt, __VA_ARGS_

is there a way to write macros with a variable argument list in visual C++?

半腔热情 提交于 2019-12-23 06:55:06
问题 As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is there a way to do that in VC++? 回答1: Yes but only since VC++ 2005. The syntax for your example would be: #define DBGPRINT(fmt, ...) printf(fmt, __VA_ARGS__) A full reference is here. 回答2: Yes, you can do this in Visual Studio C++ in versions 2005 and beyond (not sure about VS 2003). Take a look at VA_ARGS . You can basically do something like this: #define DBGPRINTF(fmt, ...) printf(fmt, __VA_ARGS_

Cleanest way to store lists of filter coefficients in a C header

旧城冷巷雨未停 提交于 2019-12-23 05:41:41
问题 I have many (~100 or so) filter coefficients calculated with the aid of some Matlab and Excel that I want to dump into a C header file for general use, but I'm not sure what the best way to do this would be. I was starting out as so: #define BUTTER 1 #define BESSEL 2 #define CHEBY 3 #if FILT_TYPE == BUTTER #if FILT_ROLLOFF == 0.010 #define B0 256 #define B1 512 #define B2 256 #define A1 467 #define A2 -214 #elif FILT_ROLLOFF == 0.015 #define B0 256 #define B1 512 // and so on... However, if I

C++ method declaration including a macro

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 04:37:11
问题 I'm using QuickFAST library and while checking it I found this class declaration which I don't seem to really get ! I mean what does a macro name before the class name ! class QuickFAST_Export Message : public FieldSet also I found this declaration friend void QuickFAST_Export intrusive_ptr_add_ref(const Field * ptr); and again I don't get the use of this declaration ! for more info here's the QuickFAST_Export.hpp #ifdef _MSC_VER # pragma once #endif #ifndef QUICKFAST_EXPORT_H #define

How to detect X32 ABI or environment in the preprocessor?

老子叫甜甜 提交于 2019-12-23 02:58:21
问题 X32 is an ABI for amd64 / x86_64 CPUs using 32-bit pointers. The idea is to combine the larger register set of x86_64 with the smaller memory and cache footprint resulting from 32-bit pointers. It provides up to about a 40% speedup. See Difference between x86, x32, and x64 architectures on Stack Overflow, and the Debian X32 Ports wiki page for details and setting it up as a chroot environment. We have a bug report from a Debian maintainer under the environment. The report is adcq is an