c-preprocessor

Portable C SWAP macro which doesn't require a 'type' argument and doesn't use memcpy

孤街浪徒 提交于 2019-12-30 12:57:07
问题 Swap macro's which take a type are fairly well known. #define SWAP(type, a_, b_) do { \ type SWAP, *a = &(a_), *b = &(b_); \ SWAP = *a; \ *a = *b; \ *b = SWAP; \ } while (0) also: Macro SWAP(t,x,y) exchanging two arguments of type t Is it possible to implement this functionality while being... portable (no compiler specific typeof ) without using function calls such as memcpy (which isn't assured to get optimized out, it wasn't in my tests at least ) I came up with a flawed method which uses

What is this macro for at the beginning of a class definition?

非 Y 不嫁゛ 提交于 2019-12-30 10:37:10
问题 I'm looking through the source of a library and many classes are defined using the following form class THING_API ClassName { ... Jumping to the macro definition . . . #ifndef THING_API #define THING_API /**< This macro is added to all public class declarations. */ #endif What could this be for, and is it a common technique? 回答1: It looks to me very much like export macro, which is required when building a shared library (.dll) on Windows. When compiling with MSVC, You have to put __declspec

Preprocessor counter macro

痞子三分冷 提交于 2019-12-30 09:20:17
问题 Is there a way to create a COUNTER() macro (which follows the C++11/14 standard) that is expanded to a number which increases by one every time COUNTER() is invoked? I've thought about it, but couldn't find a way to make it work. I didn't find a way to store "state" in the COUNTER() macro. Example: #define COUNTER() <...> // Implementation goes here... #define UNIQUE_NAME_1() TEST ## COUNTER() #define UNIQUE_NAME_2() TEST ## COUNTER() // Note how the COUNTER() macro can be used with other

Use a template parameter in a preprocessor directive?

混江龙づ霸主 提交于 2019-12-30 06:25:23
问题 Is it possible to use a non-type constant template parameter in a preprocessor directive? Here's what I have in mind: template <int DING> struct Foo { enum { DOO = DING }; }; template <typename T> struct Blah { void DoIt() { #if (T::DOO & 0x010) // some code here #endif } }; When I try this with something like Blah<Foo<0xFFFF>> , VC++ 2010 complains something about unmatched parentheses in the line where we are trying to use #if . I am guessing the preprocessor doesn't really know anything

Can visual studio automatically indent / format preprocessing directives? [duplicate]

狂风中的少年 提交于 2019-12-30 06:02:56
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to force indentation of C# conditional directives? Say I want to type this in Visual Studio: class Program { private const Byte NUM_THREADS = #if DEBUG 1; #else 8; #endif } If I simply type it out (i.e. not manually fix any indentation), Visual Studio will format it like this: class Program { private const Byte NUM_THREADS = #if DEBUG 1; #else 8; #endif } Is there anything I can do so it automatically

MinGW not defining WIN32, error in preprocessor directives

爱⌒轻易说出口 提交于 2019-12-30 04:24:51
问题 I am using mingw32-make to compile a project to Windows, which depends on a project called libevent2. I keep receiving this error - util.h:69:25: fatal error: sys/socket.h: No such file or directory Obviously a file from the Linux API is trying to be included, which won't work on Windows. Upon further investigation I find however that this file should only be included if WIN32 isn't defined. #ifdef WIN32 #include <winsock2.h> #else #include <sys/socket.h> #endif 回答1: You should use _WIN32 and

Printing name and value of a macro

落爺英雄遲暮 提交于 2019-12-30 00:41:12
问题 I have a C program with a lot of optimizations that can be enabled or disabled with #define s. When I run my program, I would like to know what macros have been defined at compile time. So I am trying to write a macro function to print the actual value of a macro. Something like this: SHOW_DEFINE(X){\ if( IS_DEFINED(X) )\ printf("%s is defined and as the value %d\n", #X, (int)X);\ else\ printf("%s is not defined\n", #X);\ } However I don't know how to make it work and I suspect it is not

Why do all the C files written by my lecturer start with a single # on the first line?

时光总嘲笑我的痴心妄想 提交于 2019-12-29 10:03:32
问题 I'm going through some C course notes, and every C program source file begins with a single # on the first line of the program. Then there are blank lines, and following that other stuff followed by the main function. What is the reason for the # ? (It's out of term now and I can't really ask the chap.) Here's an example: # #include <stdio.h> int main() { printf("Hello, World!"); return 0; } 回答1: Wow, this requirement goes way back to the 1970s. In the very early days of pre-standardised C,

What does this #define syntax mean?

主宰稳场 提交于 2019-12-29 08:56:07
问题 I came across this one, don't understand. #define IDEBUG(a...) What does the "(a...)" mean? 回答1: That's a variadic macro. Quoting verbatim from the linked page: A macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of a function. Here is an example: #define eprintf(...) fprintf (stderr, __VA_ARGS__) This kind of macro is called variadic. When the macro is invoked, all the tokens in its argument list after

How many passes does the C preprocessor make?

瘦欲@ 提交于 2019-12-29 07:39:31
问题 How many passes does the C preprocessor make over the code? I tested following code on gcc 4.7.2 #define a 5 #define b a #define c b #define d c #define e d #define f e #define g f #define h g #define j h #define k j #define l k #define m l int main(void) {return d;} There is no error: $ gcc -E 1.c # 1 "1.c" # 1 "<command-line>" # 1 "1.c" # 14 "1.c" int main(void) {return 5;} Is it standard behaviour? 回答1: The C preprocesor keeps going until there's nothing more to expand. It isn't a question