stringification

Is there a way to use C++ preprocessor stringification on variadic macro arguments?

懵懂的女人 提交于 2019-11-27 22:58:13
My guess is the answer to this question is no, but it would be awesome if there was a way. To clarify, assume I have the following macro: #define MY_VARIADIC_MACRO(X...) // Does some stuff here in the macro definition What I would like to do is somehow perform stringification on all the variables of X before passing it to a variadic function; the keyword here is before. I realize there's no way to really access the individual arguments from within the macro definition, but is there a way to stringify all the arguments, with maybe something like the following? #define MY_VARIADIC_MACRO(X...)

How to convert concatenated strings to wide-char with the C preprocessor?

◇◆丶佛笑我妖孽 提交于 2019-11-27 15:12:33
I am working on a project where I have many constant strings formed by concatenation (numbers, etc.). For example, I have a LOCATION macro that formats __FILE__ and __LINE__ into a string that I can use to know where I am in the code, when printing messages or errors: #define _STR(x) # x #define STR(x) _STR(x) #define LOCATION __FILE__ "(" STR(__LINE__) ")" So, this would format a location like "file.cpp(42)". The problem is when I try to convert the result to a wide-string: #define _WIDEN(x) L ## x #define WIDEN(x) _WIDEN(x) #define WLOCATION WIDEN(LOCATION) This works just fine with GCC, and

Opposite of C preprocessor “stringification”

你离开我真会死。 提交于 2019-11-27 13:52:39
When using C preprocessor one can stringify macro argument like this: #define TO_STRING(x) "a string with " #x and so when used, the result is as follows: TO_STRING(test) will expand to: "a string with test" Is there any way to do the opposite? Get a string literal as an input argument and produce a C identifier? For example: TO_IDENTIFIER("some_identifier") would expand to: some_identifier Thank you for your answers. EDIT: For those wondering what do I need it for: I wanted to refer to nodes in a scene graph of my 3D engine by string identifiers but at the same time avoid comparing strings in

C Macros to create strings

跟風遠走 提交于 2019-11-27 04:03:16
Alternative Titles (to aid search) Convert a preprocessor token to a string How to make a char string from a C macro's value? Original Question I would like to use C #define to build literal strings at compile time. The string are domains that change for debug, release etc. I would like to some some thing like this: #ifdef __TESTING #define IV_DOMAIN domain.org //in house testing #elif __LIVE_TESTING #define IV_DOMAIN test.domain.com //live testing servers #else #define IV_DOMAIN domain.com //production #endif // Sub-Domain #define IV_SECURE "secure.IV_DOMAIN" //secure.domain.org etc #define

Stringifying template arguments

浪尽此生 提交于 2019-11-27 00:22:28
问题 Is it possible in C++ to stringify template arguments? I tried this: #define STRINGIFY(x) #x template <typename T> struct Stringify { Stringify() { cout<<STRINGIFY(T)<<endl; } }; int main() { Stringify<int> s; } But what I get is a 'T', and not an 'int'. Seems that the preprocessors kicks in before template resolution. Is there any other way to do this? Is there any way for the preprocessing to take place after template resolution? (Compiler is VC++). 回答1: You could try typeid(T).name() Edit

Concatenate int to string using C Preprocessor

廉价感情. 提交于 2019-11-27 00:11:50
I'm trying to figure out how I can concatenate a #define 'd int to a #define 'd string using the C Preprocessor. My compiler is GCC 4.1 on CentOS 5. The solution should also work for MinGW. I'd like to append a version number onto a string, but the only way I can get it to work is to make a copy of the version number defines as strings. The closest thing I could find was a method of quoting macro arguments, but it doesn't work for #define s This is does not work. #define MAJOR_VER 2 #define MINOR_VER 6 #define MY_FILE "/home/user/.myapp" #MAJOR_VER #MINOR_VER It doesn't work without the # s

What are some tricks I can use with macros? [closed]

主宰稳场 提交于 2019-11-26 23:53:06
问题 In our legacy code, as well as our modern code, we use macros to perform nifty solutions like code generations, etc. And we make use of both the # and ## operators. I am curious how other developers use macros to do cool things, if they use them at all. 回答1: In C, it's common to define macros that do some stuff getting the verbatim argument, and at the same time define functions to be able to get the address of it transparently. // could evaluate at compile time if __builtin_sin gets //

What does ## mean for the C(C++) preprocessor?

余生长醉 提交于 2019-11-26 17:59:08
I have a C program below: #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } when I run just the preprocessor it expands this as { int var12=100; printf("%d",var12); } which is the reason why the output is 100. Can anybody tell me how/why the preprocessor expands var##12 to var12 ? nothing too fancy: ## tells the preprocessor to concatenate the left and right sides see http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation because ## is a token concatenation operator for the c preprocessor. Or maybe I don't understand the question. ## is Token Pasting Operator The

How to convert concatenated strings to wide-char with the C preprocessor?

和自甴很熟 提交于 2019-11-26 17:07:19
问题 I am working on a project where I have many constant strings formed by concatenation (numbers, etc.). For example, I have a LOCATION macro that formats __FILE__ and __LINE__ into a string that I can use to know where I am in the code, when printing messages or errors: #define _STR(x) # x #define STR(x) _STR(x) #define LOCATION __FILE__ "(" STR(__LINE__) ")" So, this would format a location like "file.cpp(42)". The problem is when I try to convert the result to a wide-string: #define _WIDEN(x)

Opposite of C preprocessor “stringification”

有些话、适合烂在心里 提交于 2019-11-26 16:30:39
问题 When using C preprocessor one can stringify macro argument like this: #define TO_STRING(x) "a string with " #x and so when used, the result is as follows: TO_STRING(test) will expand to: "a string with test" Is there any way to do the opposite? Get a string literal as an input argument and produce a C identifier? For example: TO_IDENTIFIER("some_identifier") would expand to: some_identifier Thank you for your answers. EDIT: For those wondering what do I need it for: I wanted to refer to nodes