c-preprocessor

Conditional value for a #define

风格不统一 提交于 2019-12-11 23:19:57
问题 I want to define a macro kDependentMacro to be 38 if the macro kIndependentMacro is defined and 40 otherwise. What is the simplest way to do that? 回答1: #ifdef kIndependentMacro # define kDependentMacro 38 #else # define kDependentMacro 40 #endif 来源: https://stackoverflow.com/questions/3041300/conditional-value-for-a-define

Is there an easier way to do a macro to define a function with variable amount of arguments?

北慕城南 提交于 2019-12-11 18:47:34
问题 I have a macro that defines a function with a variable amount of arguments, the macro has some logic to decide which real function must be called. My current approach is the following: #define FUNC(ret,args,args_call) \ ret my_func(args) { \ if( something ) other_func(args_call);\ return one_func(args_call);\ } #define PARAM(...) __VA_ARGS__ I use it like that: class AClass : public AInterface { public: FUNC(int,PARAM(int a, int b),PARAM(a,b)) }; I was wondering if there is a better way to do

Using nested macro with different number of arguments in C++

点点圈 提交于 2019-12-11 17:43:50
问题 The following code fails in compilation by g++ -std=c++11 compiler. # include<iostream> # include<vector> using namespace std; # define stlf(x) x.begin(), x.end() # define repf(it, a, b) for(auto it = a ; it != b ; ++it) /* // Also, following alternative fails # define repf(it, a, b) for(auto it = a ; it != b ; ++it) # define stlf(x) x.begin(), x.end() */ typedef vector<int > vi; # define pd(x) printf("%d", x); int main(void){ vi arr(10, -1); repf(arr, stlf(arr)) pd(arr[i]); return 0; } 1.

How to do a runtime subclassing system

北城余情 提交于 2019-12-11 16:26:04
问题 I am doing a subclassing system which may be defined in runtime. I have a subclass that forwards the method for a table ( std::map ), if a method is not available in the table, the super class method is used. Example (function parameters and return type are not the problem, I just simplified it): class Superclass { public: virtual void doSomething(); }; class Superclass_subclass : public Superclass { public: std::map< std::string,std::function<void (Superclass_subclass*)> > table; int

Preprocessor Include Paths, Macros etc. entry not available

一世执手 提交于 2019-12-11 15:03:46
问题 I can't navigate through a project in Eclipse (Oxygen.3a Release (4.7.3a) with CDT version 9.4.3.201802261533). The project is built successfully but I can't trace back many functions using ctrl+LMclick. I tried fixing the problem from online resources, but many suggested modifying the entry of Preprocessor Include Paths, Macros etc, which my project does not even have. What is the reason behind this and how do I fix it? For some reason, the next entry (C/C++ Include Paths and Symbols) shows

Check a __declspec macro in preprocessor

无人久伴 提交于 2019-12-11 14:10:49
问题 If I have SOME_MACRO which is defined as either __declspec(dllimport) or __declspec(dllexport) , is there a way to check at compile time which one is being used? I.e. something like this: #if SOME_MACRO == __declspec(dllimport) // do something #else // do something else #endif UPD. Looking at the answers I'm getting I guess I should be more specific in why I need this. I'm trying to compile a rather big 3rd party library, which has a function declared as dllexport in most parts of their code

Stack overflow from local variables?

坚强是说给别人听的谎言 提交于 2019-12-11 13:50:10
问题 Let me start by saying my question is not about stack overflows but on way to make it happen, without compile-time errors\warnings. I know (first hand) you can overflow a stack with recursion: void endlessRecursion() { int x = 1; if(x) endlessRecursion(); //the 'if' is just to hush the compiler } My question is, is it possible to overflow the stack by declaring too many local variables. The obvious way is just declare a huge array like so: void myStackOverflow() { char maxedArrSize[0x3FFFFFFF

Pre-Processor directives in C#

风格不统一 提交于 2019-12-11 13:32:32
问题 I seem to be having trouble with preprocessor directives in C#. I've created a Visual Studio 2008 C# win forms app. I add this: #if (DEBUG) textBox1.Text = "in debug mode"; #else textBox1.Text = "in release mode"; #endif And when I run in debug I see the expected "in debug mode". However when I switch to Release, compile, and run the .exe, I still see the "in debug mode" text. In my project properties I have Define DEBUG constant checked. I even get the correct color-coded syntax for the code

Once-only pseudo-generic header in C

雨燕双飞 提交于 2019-12-11 13:30:06
问题 After some work on the generic vector I asked about on this question, I would like to know if there is any way of checking that each instanciation of the library is only done once per type. Here is what the current header file looks like: #ifndef VECTOR_GENERIC_MACROS #define VECTOR_GENERIC_MACROS #ifndef TOKENPASTE #define TOKENPASTE(a, b) a ## b #endif #define vector_t(T) TOKENPASTE(vector_t_, T) #define vector_at(T) TOKENPASTE(*vector_at_, T) #define vector_init(T) TOKENPASTE(vector_init_,

GNU C preprocessor: Stringify the result of a macro evaluation

北战南征 提交于 2019-12-11 12:59:05
问题 I have a common string macro that I want to convert to a length-value string, all within macros, if possible, so everything ends up in .rodata . #define PAYLOAD "xyz" #define PAYLOAD_LEN (sizeof(PAYLOAD)-1) I would like to use PAYLOAD_LEN, as a string, in part of another string, e.g. const char lv_macro[] = "<preamble>" PAYLOAD_LEN ":" PAYLOAD; const char lv_wanted[] = "<preamble>3:xyz"` I suspect that this is not possible, and that I should just define PAYLOAD_LEN as a literal, e.g. #define