c-preprocessor

Valid characters for file name in the #include directive

一世执手 提交于 2019-12-10 18:41:14
问题 What are the valid characters for the file name in the #include directive? Under Linux, for example, I can use pretty much any character except / and \0 to have a valid file name, but I would expect C preprocessor to be more restrictive about the names of files I can include. 回答1: In C++, the source character set is defined in [lex.charset]: The basic source character set consists of 96 characters: the space character, the control characters representing horizontal tab, vertical tab, form

Is it possible to prevent duplicate, identical arguments to a macro in C?

你离开我真会死。 提交于 2019-12-10 18:09:06
问题 There are certain rare cases, it may be useful to prevent duplicate arguments to a macro. Take this ELEM(value, ...) macro, To check if value is either A , B or C . if (ELEM(value, A, B, C)) { .... } Someone could, by accident passes in the same argument multiple times, eg: if (ELEM(value, A, B, B)) { .... } While valid C, but almost certainly an accident, and highly unlikely to be what the developer intended. ... this is of course a trivial example, actual error cases would be more

Defining path using #define in C

℡╲_俬逩灬. 提交于 2019-12-10 18:01:22
问题 I want to define a path like this: #define PATH /abc/xyz/lmn This PATH is a directory which has files foo1, foo2, foo3, ... foo115. How can I use this #define in the "open" call to open foo1, foo2, ... foo115 ? I want to basically do this using the directive: fd = open("/abc/xyz/lmn/foo1", O_RDONLY); 回答1: #define PATH "/abc/xyz/lmn" int main (int argc, char **argv) { char file2open[256]; int i; for (i = 1; i <= 115; i++) { sprintf (file2open, "%sfoo%d", PATH, i); fd = open (file2open, O

How to check if openssl or cryptopp is installed and use the library that actually exists in the system (is installed)?

半城伤御伤魂 提交于 2019-12-10 17:50:29
问题 I wrote function that encrypts/decrypts a buffer (2 versions of the same function - first, with cryptopp, second - with openssl). I would like to make something like this: #if defined OPENSSL run_aes_openssl(...); #elif defined CRYPTOPP run_aes_crytopp(...); #else error(...); #end Is it possible? 回答1: It's not quite that simple. In order to find that a macro is defined, you have to include the header that defines that macro. And C doesn't have anything like "include foo.h iff it exists"; it

Predefined macros for method names

夙愿已清 提交于 2019-12-10 17:49:24
问题 In C++ there are predefined macros such as __FUNCTION__ , which compile to a string literal of the function name the macro is used in. void MyFunc() { printf("I'm in %s!", __FUNCTION__); // I'm in MyFunc! } Is there anything similar for C#? I am looking to do this for asp.net web forms: public string MyProperty { get { return (string)ViewState[__PROPERTY__]; } set { ViewState[__PROPERTY__] = value; } } Obviously this doesn't work (otherwise I wouldn't ask the question), I would like to know

Macro to call a function

大兔子大兔子 提交于 2019-12-10 17:46:06
问题 I need a macro (or a function, but preferably a macro) that takes a function name and an unlimited number of arguments, then passes the arguments to the function. Let's say this macro is MACROFOO . #define MACROFOO(function, ...) /* what do I put here?? */ int foo_bar(int x, int y) { // do stuff } int main(void) { int x = 3; int y = 5; MACROFOO(foo_bar, x, y); // calls foo_bar(x, y) } How could I define such a macro? I thought of doing something like: #define MACROFOO(function, args...)

C++ preprocessor test if class member exists

北战南征 提交于 2019-12-10 17:38:16
问题 Is there an equivalent of #ifdef to test if a member exists in a class so that processing can be done without causing the code to fail the compiler. I have tried template operations but the particular problem has not succeeded. For example #if member baseclass.memberA() baseclass.memberA().push_back(data); #else doAlternate(data); #endif Obviously the above is not valid, but I am trying to discover if something like this has been added to C++11 Note that in the initial setup, there will exist

stringify arbitrary number of variables

孤者浪人 提交于 2019-12-10 17:23:47
问题 For a single variable (or a given number of variables), it's easy to use macros to stringify variables. E.g. for 2 variables, I can do: #define STRINGIFY(var1, var2) (std::string(#var1) + " " + #var2) Is there a way to extend the above with either variadic macros or some other sort of compile-time trickery, to ultimately get a STRINGIFY function that accepts an arbitrary number of arguments? 回答1: I'm not sure that I understood what you're trying to do. The code below tokenizes, at compile

Too many actual parameters for macro?

故事扮演 提交于 2019-12-10 17:19:10
问题 Code: #include <iostream> using namespace std; #define ADD(x,y) ((x)+(y)) int main( int argc, char** argv ) { cout << ADD(1,2,) << endl; return 0; } Compiler output: 1>Compiling... 1>main.cpp 1>c:\warn_test\main.cpp(9) : warning C4002: too many actual parameters for macro 'ADD' Why isn't this an error? g++ (GCC) 4.2.1 20070719 [FreeBSD] gives more reasonable (in my mind) output: main.cpp:9:18: error: macro "ADD" passed 3 arguments, but takes just 2 main.cpp: In function 'int main(int, char**)

How to setup Xcodes “debug/release target settings”?

为君一笑 提交于 2019-12-10 17:18:51
问题 I have found a set of great macros here Objective C Macros I put the: #if DEBUG==1 #define .... macros in my header file. Now I simply can't figure out where to set DEBUG=1 or DEBUG=0 in Xcode so that it will define the macro when debugging and not when releasing. Hope someone can help me find the missing drop down menu:) 回答1: EDIT: Ok apparently, when working on an iPhone application, you have to: select iPhone device as the active sdk then edit project or target settings and in "Other C