C/C++ macro expansion - c++

I have the following code:
#define UNIT_BASIC_UNIT_DEF2 (name) UNIT_BASIC_ ## name
#define UNIT_UNIT_TYPE_DEF2 (basic_type, name) UNIT_ ## basic_type ## _ ## name
#define UNIT_BASIC_UNIT_CLASS_NAME2(name) CUnit ## name
#define UNIT_UNIT_TYPE_CLASS_NAME2(basic_type, name) CUnit ## basic_type ## _ ## name
#define UNIT_BASIC_UNIT_DEF (name) UNIT_BASIC_UNIT_DEF2(name)
#define UNIT_UNIT_TYPE_DEF (basic_type, name) UNIT_UNIT_TYPE_DEF2 (basic_type, name)
#define UNIT_BASIC_UNIT_CLASS_NAME(name) UNIT_BASIC_UNIT_CLASS_NAME2(name)
#define UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name) UNIT_UNIT_TYPE_CLASS_NAME2(basic_type, name)
#define UNIT_IMPLEMENT_UNIT_TYPE(basic_type, name) \
CUnitAbstract& UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name)::dup(){\
UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name) * n = new UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name)(this->value);\
return *n;\
}\
CUnitAbstract& UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name) ::operator+(CUnitAbstract& value){\
DYNAMIC_ASSERT(dynamic_cast< UNIT_BASIC_UNIT_CLASS_NAME(basic_type) *>(&value) != NULL);\
CUnitAbstract * tmp = &this->dup();\
*tmp = this->value + conversionTable[UNIT_BASIC_UNIT_DEF(basic_type)][UNIT_UNIT_TYPE_DEF(basic_type, name)] * value.getInBasicUnit();return *tmp;\
}
When I'm calling the macro with
UNIT_IMPLEMENT_UNIT_TYPE(DISTANCE, METER)
I get compilation errors:
error: 'basic_type' was not declared in this scope
error: 'name' was not declared in this scope
error: expected ']' before 'UNIT_basic_type_name'
error: expected ';' before 'UNIT_basic_type_name'
meaning that the macro wasn't expanded as I wanted it to in the last line inside the brackets.
What did I do wrong?

#define UNIT_BASIC_UNIT_DEF2 (name) UNIT_BASIC_ ## name
#define UNIT_UNIT_TYPE_DEF2 (basic_type, name) UNIT_ ## basic_type ## _ ## name
...
#define UNIT_BASIC_UNIT_DEF (name) UNIT_BASIC_UNIT_DEF2(name)
#define UNIT_UNIT_TYPE_DEF (basic_type, name) UNIT_UNIT_TYPE_DEF2 (basic_type, name)
Remove the spaces between the macro name and the argument list. These should be:
#define UNIT_BASIC_UNIT_DEF2(name) ...
// ^^^
// no space here!
When there's a space like this:
#define FOO (params) replacement
that defines an object-like macro, where the symbol gets directly replaced by the replacement list, with no parameter substitution. So an invocation like this:
FOO(bar)
gets expanded to this:
(params) replacement
By removing the space, you get a function-like macro as intended:
#define FOO(params) replacement
and FOO(bar) gets correctly expanded to replacement.

Related

C macro that concats two templated types [duplicate]

I faced a problem - I need to use a macro value both as string and as integer.
#define RECORDS_PER_PAGE 10
/*... */
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " #RECORDS_PER_PAGE \
" LIMIT " #RECORDS_PER_PAGE ";"
char result_buffer[RECORDS_PER_PAGE][MAX_RECORD_LEN];
/* ...and some more uses of RECORDS_PER_PAGE, elsewhere... */
This fails with a message about "stray #", and even if it worked, I guess I'd get the macro names stringified, not the values. Of course I can feed the values to the final method ( "LIMIT %d ", page*RECORDS_PER_PAGE ) but it's neither pretty nor efficient.
It's times like this when I wish the preprocessor didn't treat strings in a special way and would process their content just like normal code.
For now, I cludged it with #define RECORDS_PER_PAGE_TXT "10" but understandably, I'm not happy about it.
How to get it right?
The xstr macro defined below will stringify after doing macro-expansion.
#define xstr(a) str(a)
#define str(a) #a
#define RECORDS_PER_PAGE 10
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " xstr(RECORDS_PER_PAGE) \
" LIMIT " xstr(RECORDS_PER_PAGE) ";"
#include <stdio.h>
#define RECORDS_PER_PAGE 10
#define TEXTIFY(A) #A
#define _REQUEST_RECORDS(OFFSET, LIMIT) \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " TEXTIFY(OFFSET) \
" LIMIT " TEXTIFY(LIMIT) ";"
#define REQUEST_RECORDS _REQUEST_RECORDS(RECORDS_PER_PAGE, RECORDS_PER_PAGE)
int main() {
printf("%s\n", REQUEST_RECORDS);
return 0;
}
Outputs:
SELECT Fields FROM Table WHERE Conditions OFFSET %d * 10 LIMIT 10;
Note the indirection to _REQUEST_RECORDS to evaluate the arguments before stringifying them.
Try double escaping your quotes
#define RECORDS_PER_PAGE 10
#define MAX_RECORD_LEN 10
/*... */
#define DOUBLEESCAPE(a) #a
#define ESCAPEQUOTE(a) DOUBLEESCAPE(a)
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " ESCAPEQUOTE(RECORDS_PER_PAGE) \
" LIMIT " ESCAPEQUOTE(RECORDS_PER_PAGE) ";"
char result_buffer[RECORDS_PER_PAGE][MAX_RECORD_LEN];
int main(){
char * a = REQUEST_RECORDS;
}
compiles for me. The token RECORDS_PER_PAGE will be expanded by the ESCAPEQUOTE macro call, which is then sent into DOUBLEESCAPE to be quoted.

Is there any way to wrap a token in quotes with a #define? [duplicate]

I faced a problem - I need to use a macro value both as string and as integer.
#define RECORDS_PER_PAGE 10
/*... */
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " #RECORDS_PER_PAGE \
" LIMIT " #RECORDS_PER_PAGE ";"
char result_buffer[RECORDS_PER_PAGE][MAX_RECORD_LEN];
/* ...and some more uses of RECORDS_PER_PAGE, elsewhere... */
This fails with a message about "stray #", and even if it worked, I guess I'd get the macro names stringified, not the values. Of course I can feed the values to the final method ( "LIMIT %d ", page*RECORDS_PER_PAGE ) but it's neither pretty nor efficient.
It's times like this when I wish the preprocessor didn't treat strings in a special way and would process their content just like normal code.
For now, I cludged it with #define RECORDS_PER_PAGE_TXT "10" but understandably, I'm not happy about it.
How to get it right?
The xstr macro defined below will stringify after doing macro-expansion.
#define xstr(a) str(a)
#define str(a) #a
#define RECORDS_PER_PAGE 10
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " xstr(RECORDS_PER_PAGE) \
" LIMIT " xstr(RECORDS_PER_PAGE) ";"
#include <stdio.h>
#define RECORDS_PER_PAGE 10
#define TEXTIFY(A) #A
#define _REQUEST_RECORDS(OFFSET, LIMIT) \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " TEXTIFY(OFFSET) \
" LIMIT " TEXTIFY(LIMIT) ";"
#define REQUEST_RECORDS _REQUEST_RECORDS(RECORDS_PER_PAGE, RECORDS_PER_PAGE)
int main() {
printf("%s\n", REQUEST_RECORDS);
return 0;
}
Outputs:
SELECT Fields FROM Table WHERE Conditions OFFSET %d * 10 LIMIT 10;
Note the indirection to _REQUEST_RECORDS to evaluate the arguments before stringifying them.
Try double escaping your quotes
#define RECORDS_PER_PAGE 10
#define MAX_RECORD_LEN 10
/*... */
#define DOUBLEESCAPE(a) #a
#define ESCAPEQUOTE(a) DOUBLEESCAPE(a)
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " ESCAPEQUOTE(RECORDS_PER_PAGE) \
" LIMIT " ESCAPEQUOTE(RECORDS_PER_PAGE) ";"
char result_buffer[RECORDS_PER_PAGE][MAX_RECORD_LEN];
int main(){
char * a = REQUEST_RECORDS;
}
compiles for me. The token RECORDS_PER_PAGE will be expanded by the ESCAPEQUOTE macro call, which is then sent into DOUBLEESCAPE to be quoted.

base file name from __FILE__

I need the file name only where the __FILE__ and __FILEW__ macros return the whole path.
I defined the following:
#define __FILE_NAME_ONLY__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
I am using it as follows:
#define MY_TRACE( mask, format, ... ) \
GlobalTrace( mask, L"-[" __FILE_NAME_ONLY__ L":" format , __VA_ARGS__ )
I get the following error:
error C2064: term does not evaluate to a function taking 1 arguments
and when I try the following macros:
#define __WIDE(_String) L ## _String
#define _WIDE(_String) __WIDE(_String)
as follows
#define MY_TRACE( mask, format, ... ) \
GlobalTrace( mask, L"-[" _WIDE(__FILE_NAME_ONLY__) L":" format , __VA_ARGS__ )
I get : error C2146: syntax error : missing ')' before identifier 'L' when I actually try to use the MY_TRACE macro
what am I missing? Thanks
You're depending on string literal concatenation, except that all of the terms aren't string literals.
I assume you were previously doing it like so:
#define MY_TRACE( mask, format, ... ) \
GlobalTrace( mask, L"-[" __FILE__ L":" format , __VA_ARGS__ )
If __FILE__ and format expand to a string literal, the 4 strings get pasted together into one. "A" "B" "C" "D" is the same as "ABCD".
That doesn't happen when you replace with __FILE_NAME_ONLY__ because it expands to a function call, not a literal.
If you're using gcc this macro should help:
__BASE_FILE__
Here's a helpful list

De-duplication of NSString & unichar constants

I have two simple constants:
NSString and
unichar,
defined as follows:
static NSString * const string = #"\u2022";
static unichar const character = 0x2022;
I'd want to have the number 0x2022 defined in one place.
I tried a lot of combinations (# and ##, also CFSTR macro) with no success.
Can it be done?
(Using ideas from How to concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?):
#define STRINGIFY(_x_) #_x_
#define UNICHAR_FROM_HEXCODE1(_c_) 0x ## _c_
#define UNICHAR_FROM_HEXCODE(_c_) UNICHAR_FROM_HEXCODE1(_c_)
#define NSSTRING_FROM_HEXCODE1(_c_) #"" STRINGIFY(\u ## _c_)
#define NSSTRING_FROM_HEXCODE(_c_) NSSTRING_FROM_HEXCODE1(_c_)
#define MYCHARCODE 2022
static unichar const character = UNICHAR_FROM_HEXCODE(MYCHARCODE);
static NSString * const string = NSSTRING_FROM_HEXCODE(MYCHARCODE);
Preprocessed output:
static unichar const character = 0x2022;
static NSString * const string = #"" "\u2022";
(Note that #"" "\u2022" is equivalent to #"\u2022".)

Concatenating #define in C++

I have something like:
#define BASE_FOLDER = "Resources"
#define PREFERENCE_FILE_NAME = "AppPreferences.txt"
#define SPLASH_FILE_NAME = "Splash.png"
#define PREFERENCE_PATH = ".\\" + BASE_FOLDER + "\\" + PREFERENCE_FILE_NAME
#define SPLASH_PATH = ".\\" + BASE_FOLDER + "\\" + SPLASH_FILE_NAME
and the compiler is throwing errors where PREFERENCE_PATH is used.
expecting primary expression before = token.
These all worked when I was doing
#define PREFERENCE_PATH = ".\\Resources\\AppPreferences.txt"
#define SPLASH_PATH = ".\\Resources\\Splash.png"
What am I doing wrong?
Get rid of the equal signs. Preprocessor definitions don't use equal signs.
Then get rid of the pluses. String literals are concatenated when you put them side by side without a plus in between. Emphasis on literals because this is a compile-time feature that only works with double-quoted literals, as in "foo" "bar" → "foobar". It doesn't work with variables.
#define BASE_FOLDER "Resources"
#define PREFERENCE_FILE_NAME "AppPreferences.txt"
#define SPLASH_FILE_NAME "Splash.png"
#define PREFERENCE_PATH ".\\" BASE_FOLDER "\\" PREFERENCE_FILE_NAME
#define SPLASH_PATH ".\\" BASE_FOLDER "\\" SPLASH_FILE_NAME
You can't concatenate strings (char* that is) in C++ like that...there is no + operator for them.