Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ #define Macro inside macro?

I would like something like:

#define C_OR_CPP(C__, CPP__) #ifdef __cplusplus\
CPP__\
#else\
C__\
#endif

Is it possible? Maybe some dirty hack with #include ?

Reason: I make a header where a struct uses a member variable of type vector<stuff>*, but in C i want it to simply be void*, you know.

TIA

like image 488
imacake Avatar asked Feb 24 '12 13:02

imacake


3 Answers

What's the problem with

#ifdef __cplusplus
#define C_OR_CPP(C, CPP) CPP
#else
#define C_OR_CPP(C, CPP) C
#endif

(Leaving names with double underscore to the implementation per phresnel remark)

like image 171
AProgrammer Avatar answered Oct 16 '22 05:10

AProgrammer


My English is poor, and I'm sorry for language mistakes and typos if any.

If #ifdef must not wrap the macro invocation, there is a solution not so graceful.

g++ only: You may try this in selective occasions. But if there are commas in a or b, workarounds are still needed. It's simply based on the fact that __cplusplus is defined to "1" when in a C++ environment and remains itself while not.

#define SELECT1(a, b) a
#define SELECT__cplusplus(a, b) b
#define xcat(a,b)  a##b
#define concat(...) xcat(__VA_ARGS__)
#define C_OR_CPP(C, CPP) concat(SELECT, __cplusplus)(C, CPP)

C_OR_CPP(1, 2)

Other Environments Check the __cplusplus macro, a compiler that comforming to standard C++ should generate

 #define __cplusplus value 

and value should >= 199711L

like image 31
ringo Avatar answered Oct 16 '22 05:10

ringo


Not in C++. But you can

#ifdef __cplusplus
# define CPP
#else
# define C
#endif

I assume this is just a pathological example by you. Note also that double underscore is reserved to library implementors (see 17.6.4.3.2 Global names).

vector, but in C i want it to simply be void, you know.

So, what speaks against a solution like

struct Foo {
  #ifdef __cplusplus
  ...
  #else
  ...
  #endif
};

or what speaks against providing different APIs for different programming languages?

like image 35
Sebastian Mach Avatar answered Oct 16 '22 07:10

Sebastian Mach