c-preprocessor

Scalable automatic class registration in C++

旧巷老猫 提交于 2019-12-03 07:53:18
Automatic class registration in C++ is a common task, and a commonly asked question here on StackOverflow: Register an object creator in object factory Somehow register my classes in a list automatic registration of object creator function with a macro c++ automatic factory registration of derived types The basic objective is to register classes automatically with some registry or factory so that it can do some work with each class later. This is a well-established technique, used by libraries like (for example) Google Test ( http://code.google.com/p/googletest ), which automatically registers

C-preprocessor recursive macro

混江龙づ霸主 提交于 2019-12-03 07:42:17
问题 #define PP_ARG0_(arg0, ...) arg0 #define PP_REST_(arg0, ...) __VA_ARGS__ #define PP_ARG0(args) PP_ARG0_ args #define PP_REST(args) PP_REST_ args #define FUNCTION(name) void name(); #define FUNCTION_TABLE(...) \ FUNCTION(PP_ARG0((__VA_ARGS__))) \ FUNCTION_TABLE(PP_REST((__VA_ARGS__))) \ test code: FUNCTION_TABLE(f1, f2,f3,testA,testB,testC); Obviously, because of recursive expansion it will only declare void f1(); and the rest won't be expanded: void f1(); FUNCTION_TABLE(f2,f3,testA,testB

#if in java, like in c preprocessors [duplicate]

你说的曾经没有我的故事 提交于 2019-12-03 07:16:32
This question already has answers here : How to mark java code such that it's not compiled [duplicate] (9 answers) Possible Duplicate: How to mark java code such that it’s not compiled In c , we can prevent compilation of block code like this : #if 0 //code here #endif So even if code block is error prone the code compiles, I want the same thing in Java, So that I can skip that part of code which won't compile because some library is missing. Can anyone help me ? You have to comment out the code , you can't use pre-processor directive in java. There is no preprocessor in Java. Depending on

Why should one bother with preprocessor directives?

这一生的挚爱 提交于 2019-12-03 06:51:23
问题 This question may seem rather basic, but coming from an engineering (non computer-science) background, I was unsure about what the snippets of ' # 's were in some C++ code. A quick search led me to the concise, well-explained cplusplus tutorial page on preprocessor directives. But why bother with the concept of preprocessor directives at all? Is it not possible to write equivalent code that can assign values to constants, define subroutines/function/macros and handle errors? I guess I

What C preprocessor conditional should I use for OS X specific code?

不想你离开。 提交于 2019-12-03 06:50:00
问题 What C preprocessor conditional should I use for OS X specific code? I need to include a specific library if I am compiling for OS X or a different header if I am compiling for Linux. I know there is __APPLE__ but I don't know if that is a current conditional for OS X 10.x. 回答1: This list of operating system macros says the presence of both __APPLE__ and __MACH__ indicate OSX. Also confirmed at line 18 of part of the source for fdisk. 回答2: __APPLE__ will tell you you're compiling on an Apple

Multiple preprocessor directives on one line in C++

走远了吗. 提交于 2019-12-03 05:55:56
A hypothetical question: Is it possible to have a C++ program, which includes preprocessor directives, entirely on one line? Such a line would look like this: #define foo #ifdef foo #define bar #endif What are the semantics of such a line? Further, are there any combinations of directives which are impossible to construct on one line? If this is compiler-specific then both VC++ and GCC answers are welcome. A preprocessing directive must be terminated by a newline, so this is actually a single preprocessing directive that defines an object-like macro, named foo , that expands to the following

Precompiled Headers? Do we really need them

北城余情 提交于 2019-12-03 05:53:47
Back a long time ago I used to use pre-compiled headers: a. to speed compilation and b. because I supported multiple development tools like CodeWarrior, MPW, VS, ProjectBuilder, gcc, intel compilers, etc, etc. Now I have a Mac Pro with 32gb of RAM. Now I use just CMake. So do we really need pre-compiled headers any more? Are there obvious benefits that I just dont see/know? How can one make a cross-platform pre-compiled header? Maybe that would simplify my life too. There is no such thing as a build that is "Fast enough". Proponents of TDD ( Test-Driven Development ) will be upset if their

CUDA compiler (nvcc) macro

做~自己de王妃 提交于 2019-12-03 05:51:22
Is there a #define compiler (nvcc) macro of CUDA which I can use? (Like _WIN32 for Windows and so on.) I need this for header code that will be common between nvcc and VC++ compilers. I know I can go ahead and define my own and pass it as an argument to the nvcc compiler (-D), but it would be great if there is one already defined. __CUDACC__ I don't think it will be that trivial. Check the following thread http://forums.nvidia.com/index.php?showtopic=32369&st=0&p=179913&#entry179913 N. Pattakos I know it has been long time now, but you might also find __CUDA_ARCH__ useful. 来源: https:/

C find static array size (preventing mistakes) [duplicate]

你说的曾经没有我的故事 提交于 2019-12-03 05:33:17
This question already has an answer here: Array-size macro that rejects pointers 9 answers Finding the size of a static array is a common operation. see: C find static array size - sizeof(a) / sizeof((a)[0]) This can be wrapped into a macro, eg: #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) however its possible to accidentally pass in a regular pointer. eg: void func(SomeArray **foo) { int i = ARRAY_SIZE(foo); } While its valid C, but often ends up being a logical error. Its possible to prevent this mistake (taking advantage of per-processor to fail on a zero length bit-field) . #define

“#ifdef” inside a macro [duplicate]

烂漫一生 提交于 2019-12-03 05:28:26
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: #ifdef inside #define How do I use the character "#" successfully inside a Macro? It screams when I do something like that: #define DO(WHAT) \ #ifdef DEBUG \ MyObj->WHAT() \ #endif \ 回答1: You can't do that. You have to do something like this: #ifdef DEBUG #define DO(WHAT) MyObj->WHAT() #else #define DO(WHAT) do { } while(0) #endif The do { } while(0) avoids empty statements. See this question, for example. 回答2: