c-preprocessor

Scalable automatic class registration in C++

大憨熊 提交于 2019-12-04 11:59:54
问题 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

Equivalent of #define in tcl?

穿精又带淫゛_ 提交于 2019-12-04 11:41:20
Is there a command in tcl that is equivalent to C++ #define? I've seen ways to implement "define" using overloading of the proc function, just wanted to know if anyone knows of a more starightforward way Tcl has a mechanism that lets you define aliases to procedures in an interpreter. If you have proc foo {one two three} {do something with $one $two $three} and you find you're always passing $a and $b as the first two arguments, you can write: interp alias {} foo_ab {} foo $a $b And now you can say: foo_ab $d ;# same as "foo $a $b $d" foo_ab $e ;# same as "foo $a $b $e" example: proc foo {one

Passing a template which requires a comma to a single-argument macro

▼魔方 西西 提交于 2019-12-04 10:55:10
I have some code that essentially condenses down to #define FOO(a) FOO(std::map<int, int>); But it emits a compile error (too many actual parameters for macro FOO ). Obviously the preprocessor is thinking that I've supplied std::map<int and int> as arguments. Is there a way round this? The preprocessor will not treat a quoted string with a comma in this way. This should perhaps ideally be a comment, but SO doesn't support code in comments, so, you can do #include <map> #define T_ARGS( ... ) < __VA_ARGS__ > #define FOO( a ) a x; auto main() -> int { FOO( std::map T_ARGS( int, int ) ); (void) x;

Precompiled Headers? Do we really need them

主宰稳场 提交于 2019-12-04 10:27:34
问题 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. 回答1: There is no such

How to access C preprocessor constants in assembly?

拜拜、爱过 提交于 2019-12-04 10:00:52
If I define a constant in my C .h file: #define constant 1 How do I access it in my assembly .s file? If you use the GNU toolchain, gcc will by default run the preprocessor on files with the .S extension (uppercase 'S'). So you can use all cpp features in your assembly file. There are some caveats: there might be differences in the way the assembler and the preprocessor tokenize the input. If you #include header files, they should only contain preprocessor directives, not C stuff like function prototypes. You shouldn't use # comments, as they would be interpreted by the preprocessor. Example:

Determine #defined string length at compile time

梦想与她 提交于 2019-12-04 10:00:47
问题 I have a C-program (an Apache module, i.e. the program runs often), which is going to write() a 0-terminated string over a socket, so I need to know its length. The string is #defined as: #define POLICY "<?xml version=\"1.0\"?>\n" \ "<!DOCTYPE cross-domain-policy SYSTEM\n" \ "\"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\n" \ "<cross-domain-policy>\n" \ "<allow-access-from domain=\"*\" to-ports=\"8080\"/>\n" \ "</cross-domain-policy>\0" Is there please a way, better than using

Cancelling std::cout code lines using preprocessor

三世轮回 提交于 2019-12-04 09:23:44
One can remove all calls to printf() using #define printf . What if I have a lot of debug prints like std::cout << x << endl; ? How can I quickly switch off cout << statements in a single file using preprocessor? NullStream can be a good solution if you are looking for something quick that removes debug statements. However I would recommend creating your own class for debugging, that can be expanded as needed when more debug functionality is required: class MyDebug { std::ostream & stream; public: MyDebug(std::ostream & s) : stream(s) {} #ifdef NDEBUG template<typename T> MyDebug & operator<<

Possible to convert list of #defines into strings

与世无争的帅哥 提交于 2019-12-04 09:19:38
问题 Suppose I have a list of #define s in a header file for an external library. These #define s represent error codes returned from functions. I want to write a conversion function that can take as an input an error code and return as an output a string literal representing the actual #define name. As an example, if I have #define NO_ERROR 0 #define ONE_KIND_OF_ERROR 1 #define ANOTHER_KIND_OF_ERROR 2 I would like a function to be able to called like int errorCode = doSomeLibraryFunction(); if

How does #error in C/C++ work?

谁说我不能喝 提交于 2019-12-04 08:18:24
问题 I am guessing from # that it is only a compile-time utility. How can it be used in C/C++ programs? Did not find much about it on the internet. Any links would be helpful. 回答1: It causes the compiler (or preprocessor) to output the error message. In C++, it also renders the translation unit ill-formed (i.e., it causes compilation to fail). If you have several macros that could be defined and you want to be sure that only certain combinations of them are defined, you can use #error to cause

gcc assembler preprocessor not compatible with standard headers

百般思念 提交于 2019-12-04 08:17:23
The man page for gcc states file.s Assembler code. file.S file.sx Assembler code that must be preprocessed. And many standard include files have #ifndef __ASSEMBLY__ ... #endif wrappers to allow inclusion from assembly files. I could have sworn I've written programs before with gcc and it defined this when assembling, but now I'm running into problems. Here's some test code: test.S #include <sys/syscall.h> #include <asm/signal.h> .intel_syntax noprefix .text .global foo // int foo(int pid) foo: mov esi,SIGUSR1 mov eax,SYS_kill syscall ret When I run gcc -c test.S , it complains about all kinds