For a lot of function calls in a C app that needs some degree of debugging I wanted to add a macro to ease the typing that I had to do.
right now I am calling a function like this:
aDebugFunction(&ptrToFunction, __LINE__, "ptrToFunction", param1, param2, etc)
So I thought lets write a macro that does the first 3 parameters for me, like this:
#define SOMEDEFINE(x) &x, __LINE__, "x"
However, as most of you will immediately know, this won't work it won't replace "x" with the name that x has been given but will just pass "x" as 3rd parameter.
My knowledge of this preprocessor macro happening stuff is quite limited and thus my googling-ability is also quite useless due to not knowing where to search for exactly.
I hope one of you guys/girls could give me either a solution or point me in the right direction.
You need to use the # convert token to string command of the preprocessor. You should define your second macro this way:
#define SOMEDEFINE(x) &x, __LINE__, # x
Or if x can also be a macro call, and you want the string to contains the expansion of the macro, you need to use an auxiliary macro:
#define TOKEN_TO_STRING(TOK) # TOK
#define STRINGIZE_TOKEN(TOK) TOKEN_TO_STRING(TOK)
#define SOMEDEFINE(x) &x, __LINE__, STRINGIZE_TOKEN(x)
For example, if you have the following code:
#define SHORT_NAME a_very_very_very_long_variable_name
SOMEDEFINE(SHORT_NAME)
Then, with the first macro, it will expand to
&a_very_very_very_long_variable_name, __LINE__, "SHORT_NAME"
While, with the second macro, it will expand to:
&a_very_very_very_long_variable_name, __LINE__, "a_very_very_very_long_variable_name"
You can actually do a lot better than that: #define SOMEDEFINE( X, ... ) aDebugFunction( &(X), __LINE__, #X, __VA_ARGS__ )
You could then simply call into this code like this: SOMEDEFINE( ptrToFunction, param1, param2, etc )
And that would effectively call: aDebugFunction( &( ptrToFunction ), __LINE__, "ptrToFunction", param1, param2, etc )
Related
I am using nested macro under gcc-9.3.1.
Here is the code.
#define A(_name) _name, _name, _name
#define P(_1, _2, _name, ...) _name
#define B(...) P(__VA_ARGS__, A(something))(__VA_ARGS__)
B(k)
I expected the B(k) converted to P(k, something, something, something)(k) at first and then converted into something(k).
However, compilier told me that two arguments were too few for P, which meant that A(something) was not unfolded.
Why is that? And how can I make it unfolded?
B(k) expands to P(k, A(something))(k) which then undergoes recursive expansion. The first thing it finds there is P, which doesn't have enough arguments.
If you want this to work the way I think you do, you need to arrange for A to expand before P. You can do that by adding explicit indirect EXPAND macros:
#define EXPAND(...) __VA_ARGS__
#define B(...) EXPAND(P EXPAND((__VA_ARGS__, A(something))))(__VA_ARGS__)
This way, P won't be recognized as a macro (no following () until after the inner EXPAND is expanded (which will also expand A). You need the outer EXPAND to explicitly expand P afterwards.
I have the following defines:
#define STRING_OBJECT_1 "bird"
#define STRING_OBJECT_2 "dog"
#define STRING_OBJECT_3 "cat"
#define STRING_OBJECT_4 "human"
#define STRING_OBJECT_5 "cow"
#define STRING_OBJECT_6 "snake"
#define STRING_OBJECT_7 "penguin"
#define STRING_OBJECT_8 "monkey"
I want to get numbered STRING_OBJECT only using STRING_OBJECT_ + "(number string)", so basically not directly type STRING_OBJECT_1.
Is there anyway to use for loop with string macro in C++?
Is there anyway to use for loop with string macro in C++?
No, there isn't.
Macros are processed before source code is compiled to create object code.
The values of variables in a for loop are set at run time. Hence, they cannot make use of macros.
Your best bet is to augment your code with an array variable and use the array variable in the for loop.
#define STRING_OBJECT_1 "bird"
...
#define STRING_OBJECT_8 "monkey"
std::string object_array[] = {STRING_OBJECT_1, ..., STRING_OBJECT_8};
for ( int i = 0; ... )
{
do_something(object_array[i]);
}
No, You can't do this. macros not part of the C/C++ language.
Macros are replaced by the preprocessor by their value compiles time. There is no way you had be able to change the macro at runtime.
No comma is allowed in a macro argument because it will be treated as more than one arguments and the preprocessing will be wrong. However, we can parenthesize the argument to let preprocessor treat it as one argument. Is there a macro or other techniques which can remove the enclosing parentheses?
For example, if I define a macro like
#define MY_MACRO(a, b) ...
and use it like
MY_MACRO( A<int, double>, text );
will be wrong. use it like
MY_MACRO( (A<int, double>), text)
with a macro or technique to remove the parentheses will be fine. Boost provides BOOST_IDENTITY_TYPE macro for only types but not general cases
#define ESC(...) __VA_ARGS__
then
MY_MACRO( ESC(A<int, double>), text );
might do what you want.
This macro trick is similar to Yakk's solution but removes the need to explicitly pass in another macro as a parameter.
#include <stdio.h>
#define _Args(...) __VA_ARGS__
#define STRIP_PARENS(X) X
#define PASS_PARAMETERS(X) STRIP_PARENS( _Args X )
int main()
{
printf("without macro %d %d %d %d %d %d\n", (5,6,7,8,9,10) ); // This actually compiles, but it's WRONG
printf("with macro %d %d %d %d %d %d\n", PASS_PARAMETERS((5,6,7,8,9,10)) ); //Parameter "pack" enclosed in parenthesis
return 0;
}
Of course you could get creative by making the PASS_PARAMETERS macro into a variadic macro and pass in multiple parameter packs.
Only maybe removing parentheses:
If you need to strip one layer of parenthesis, but only if there are parenthesis to strip, this longer set of macros does the trick:
#define DEPAREN(X) ESC(ISH X)
#define ISH(...) ISH __VA_ARGS__
#define ESC(...) ESC_(__VA_ARGS__)
#define ESC_(...) VAN ## __VA_ARGS__
#define VANISH
This may be needed if you want to use MY_MACRO for different sets of datatypes:
#define MY_MACRO(a, b) DEPAREN(a), b
MY_MACRO( ({x, y, z}), text )
//> {x,y,z}, text
MY_MACRO( singlearg, text )
//> singlearg, text
How it works:
We start with DEPAREN(X) ESC(ISH X). If X has parenthesis, we get ESC(ISH(X)). If X does not have parenthesis, we get ESC(ISH X).
We then expand ESC(...) into ESC_(__VA_ARGS__), which expands the interior.
ISH(...) turns into ISH __VA_ARGS__, which strips one layer of parentheses from X. Now, regardless of whether or not X originally had parenthesis, we have ESC_(ISH X).
We now need to get rid of ISH. However, because we already defined ISH(...), we can't also define it as #define ISH . That's why we concatenate it with another token (VAN) to get VANISH X.
VANISH is defined as , so we are finally left with X, sans parentheses.
A simple hack could be to use variadic macros:
#define MY_MACRO(a, b...) ...
Then you can use it like:
MY_MACRO(text, A<int, double>)
The comma in the second argument is still interpreted as the argument separator (meaning the macro is actually called with three arguments), but it's expanded inside the macro, making the behavior the same. The variadic argument has to be last in the macro, however.
I'm using a unit test framework that relies on a REQUIRE macro for performing assertions.
Simplified, the macro works like this:
#define REQUIRE( expr ) INTERNAL_REQUIRE( expr, "REQUIRE" )
Which is defined similar to this:
#define INTERNAL_REQUIRE( expr, macroName ) \
PerformAssertion( macroName, #expr, expr );
PerformAssertion's first two parameters are of the type: const char*. The reason for the second parameter (#expr) is so the exact expression that was asserted can be logged. This is where the issue lies. The preprocessor expands the expression before it is passed as a const char *, so it's not the same expression that was originally asserted.
For instance:
REQUIRE( foo != NULL );
Would result in this call:
PerformAssertion( "REQUIRE", "foo != 0", foo != 0 );
As you can see, the expression is partially expanded, e.g. the expression foo != NULL appears in the log as foo != 0. The NULL (which is a macro defined to be 0) was expanded by the C preprocessor before building the assertions message text. Is there a way I can ignore or bypass the expansion for the message text?
EDIT: Here's the solution, for anyone curious:
#define REQUIRE( expr ) INTERNAL_REQUIRE( expr, #expr, "REQUIRE" )
#define INTERNAL_REQUIRE( expr, exprString, macroName ) \
PerformAssertion( macroName, exprString, expr );
Try making the stringifying before the call to the internal require. Your problem is that it is passed to internal require in the second expansion which expands NULL. If you make the stringifying happen before that, e.g. In the require macro, it will not expand the NULL.
Here is what's going on: since you macro where the "stringization" operator # is applied is second-level, the sequence of operations works as follows:
Preprocessor identifies the arguments of REQUIRE(NULL) and performs argument substitution as per C 6.10.3.1. At this point, the replacement looks like INTERNAL_REQUIRE( 0, "REQUIRE" ), because NULL is expanded as 0.
Preprocessor continues expanding the macro chain with INTERNAL_REQUIRE; at this point, the fact that the macro has been called with NULL is lost: as far as the preprocessor is concerned, the expression passed to INTERNAL_REQUIRE is 0.
A key to solving this problem is in this paragraph from the standard:
A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded.
This means that if you would like to capture the exact expression, you need to do it in the very first level of the macro expansion.
I need to replace
GET("any_name")
with
String str_any_name = getFunction("any_name");
The hard part is how to trim off the quote marks. Possible? Any ideas?
How about:
#define UNSAFE_GET(X) String str_##X = getFunction(#X);
Or, to safe guard against nested macro issues:
#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#define PASTE2(a, b) a##b
#define PASTE(a, b) PASTE2(a, b)
#define SAFE_GET(X) String PASTE(str_, X) = getFunction(STRINGIFY(X));
Usage:
SAFE_GET(foo)
And this is what is compiled:
String str_foo = getFunction("foo");
Key points:
Use ## to combine macro parameters into a single token (token => variable name, etc)
And # to stringify a macro parameter (very useful when doing "reflection" in C/C++)
Use a prefix for your macros, since they are all in the same "namespace" and you don't want collisions with any other code. (I chose MLV based on your user name)
The wrapper macros help if you nest macros, i.e. call MLV_GET from another macro with other merged/stringized parameters (as per the comment below, thanks!).
One approach is not to quote the name when you call the macro:
#include <stdio.h>
#define GET( name ) \
int int##name = getFunction( #name ); \
int getFunction( char * name ) {
printf( "name is %s\n", name );
return 42;
}
int main() {
GET( foobar );
}
In answer to your question, no, you can't "strip off" the quotes in C++. But as other answers demonstrate, you can "add them on." Since you will always be working with a string literal anyway (right?), you should be able to switch to the new method.