Here is a short program I wrote
#include <iostream>
#define test0 "abc"
#define test1 "def"
#define concat(x,y) x##y
int main()
{
for (int i = 0 ; i < 2 ; ++i)
std::cout << concat(test,i) << std::endl;
return 0;
}
But for some reason it doesn't compile (it concatenates i instead of i value), is there a way I can concatenate i's values instead of i's name?
test1.cpp: In function ‘int main()’:
test1.cpp:8:1: error: ‘testi’ was not declared in this scope
No. Macros are expanded before compilation (hence the term pre-processor), and can only manipulate the tokens that appear in the source code. The value of the variable isn't known until the program is run.
No.
The preprocessor (the part of the compiler that handles #define and #include) runs before any other compiler pass, and long before the program ever runs. The variable i will not have a value until the program runs.
Keep in mind that the preprocessor is little more than a text-replace tool for your program source code.
Related
I try to write a macro like following:
taken from link
and I apply same rule to my software whit out success.
I notice some difference from C and C++, but I don't understand why, the macro are preprocessor job !
also I notice some difference passing to the macro the values coming from an enumerators.
#include <stdio.h>
#define CONCAT(string) "start"string"end"
int main(void)
{
printf(CONCAT("-hello-"));
return 0;
}
the reported link used to try online the code link to a demo on ideone allow selection of different language
C is ok but changing to C++ it doesn't work.
Also in my IDE Visual Studio Code (MinGw C++) doesn't work.
My final target is write a macro to parametrize printf() function, for Virtual Console application using some escape codes. I try to add # to the macro concatenation and seems work but in case I pass an enumerator to the macro I have unexpected result. the code is :
#include <stdio.h>
#define def_BLACK_TXT 30
#define def_Light_green_bck 102
#define CSI "\e["
#define concat_csi(a, b) CSI #a ";" #b "m"
#define setTextAndBackgColor(tc, bc) printf(concat_csi(bc, tc))
enum VtColors { RESET_COLOR = 0, BLACK_TXT = 30, Light_green_bck = 102 };
int main(void){
setTextAndBackgColor(30, 102);
printf("\r\n");
setTextAndBackgColor(def_BLACK_TXT , def_Light_green_bck );
printf("\r\n");
setTextAndBackgColor(VtColors::BLACK_TXT , VtColors::Light_green_bck );
printf("\r\n");
printf("\e[102;30m");// <<--- this is the expected result of macro expansion
}
//and the output is : ( in the line 3 seems preprocessor don't resolve enum (the others line are ok) )
[102;30m
[102;30m
[VtColors::Light_green_bck;VtColors::BLACK_TXTm
[102;30m
Obviously I want use enumerators as parameter... (or I will change to #define).
But I'm curious to understand why it happens, and why there is difference in preprocessor changing from C to C++.
If anyone know the solution, many thanks.
There appears to be some compiler disagreement here.
MSVC compiles it as C++ without any issues.
gcc produces a compilation error.
The compilation error references a C++ feature called "user-defined literals", where the syntax "something"suffix gets parsed as a user-defined literal (presuming that this user-defined literal gets properly declared).
Since the preprocessor phase should be happening before the compilation phase, I conclude that the compilation error is a compiler bug.
Note that adding some whitespace produces the same result whether it gets compiled as C or C++ (and makes gcc happy):
#define CONCAT(string) "start" string "end"
EDIT: as of C++11, user-defined literals are considered to be distinct tokens:
Phase 3
The source file is decomposed into comments, sequences of
whitespace characters (space, horizontal tab, new-line, vertical tab,
and form-feed), and preprocessing tokens, which are the following:
a)
header names such as or "myfile.h"
b) identifiers
c)
preprocessing numbers d) character and string literals , including
user-defined (since C++11)
emphasis mine.
This occurs before phase 4: preprocessor execution, so a compilation error here is the correct result. "start"string, with no intervening whitespace, gets parsed as a user-defined literal, before the preprocessor phase.
to summarize the behavioral is the following: (see comment in the code)
#include <stdio.h>
#define CONCAT_1(string) "start"#string"end"
#define CONCAT_2(string) "start"string"end"
#define CONCAT_3(string) "start" string "end"
int main(void)
{
printf(CONCAT_1("-hello-")); // wrong insert double quote
printf("\r\n");
printf(CONCAT_1(-hello-)); // OK but is without quote
printf("\r\n");
#if false
printf(CONCAT_2("-hello-")); // compiler error
printf("\r\n");
#endif
printf(CONCAT_3("-hello-")); // OK
printf("\r\n");
printf("start" "-hello-" "end"); // OK
printf("\r\n");
printf("start""-hello-""end"); // OK
printf("\r\n");
return 0;
}
output:
start"-hello-"end <<<--- wrong insert double quote
start-hello-end
start-hello-end
start-hello-end
start-hello-end
I am using ## (Token-Pasting Operator) to form a function call.
According to my understanding, a simple example may look like this.
#include <iostream>
#define CALL(x) Func_##x##()
void Func_foo() { std::cout << "Hi from foo()." << std::endl; }
int main() {
std::cout << "Hello World!" << std::endl;
CALL(foo);
return 0;
}
I compiled this code with g++ -std=c++14 -O3 test.cc. The G++ version is 7.3.1.
It returns the following error.
error: pasting "Func_foo" and "(" does not give a valid preprocessing token
If I change the macro to #define CALL(x) Func_##x() (delete the second ##), the error will be solved.
Why the second ## is redundant? The ## connects strings and substitutes with macro arguments if possible. For example, I change the function name to Func_foo1(), then the macro should be #define CALL(x) Func_##x##1(). This works as my expected.
I am a little confused about the macro definition #define CALL(x) Func_##x().
A token is the smallest element of a C++ program that is meaningful to the compiler.
The ## Token-pasting operator does not concatenate arbitrary printable characters. It concatenates two tokens into one token.
Why the second ## is redundant?
Because concatenating Func_foo and () does not produce a single token.
What does this line mean? Especially, what does ## mean?
#define ANALYZE(variable, flag) ((Something.##variable) & (flag))
Edit:
A little bit confused still. What will the result be without ##?
A little bit confused still. What will the result be without ##?
Usually you won't notice any difference. But there is a difference. Suppose that Something is of type:
struct X { int x; };
X Something;
And look at:
int X::*p = &X::x;
ANALYZE(x, flag)
ANALYZE(*p, flag)
Without token concatenation operator ##, it expands to:
#define ANALYZE(variable, flag) ((Something.variable) & (flag))
((Something. x) & (flag))
((Something. *p) & (flag)) // . and * are not concatenated to one token. syntax error!
With token concatenation it expands to:
#define ANALYZE(variable, flag) ((Something.##variable) & (flag))
((Something.x) & (flag))
((Something.*p) & (flag)) // .* is a newly generated token, now it works!
It's important to remember that the preprocessor operates on preprocessor tokens, not on text. So if you want to concatenate two tokens, you must explicitly say it.
## is called token concatenation, used to concatenate two tokens in a macro invocation.
See this:
Macro Concatenation with the ## Operator
One very important part is that this token concatenation follows some very special rules:
e.g. IBM doc:
Concatenation takes place before any
macros in arguments are expanded.
If the result of a concatenation is a
valid macro name, it is available for
further replacement even if it
appears in a context in which it
would not normally be available.
If more than one ## operator and/or #
operator appears in the replacement
list of a macro definition, the order
of evaluation of the operators is not
defined.
Examples are also very self explaining
#define ArgArg(x, y) x##y
#define ArgText(x) x##TEXT
#define TextArg(x) TEXT##x
#define TextText TEXT##text
#define Jitter 1
#define bug 2
#define Jitterbug 3
With output:
ArgArg(lady, bug) "ladybug"
ArgText(con) "conTEXT"
TextArg(book) "TEXTbook"
TextText "TEXTtext"
ArgArg(Jitter, bug) 3
Source is the IBM documentation. May vary with other compilers.
To your line:
It concatenates the variable attribute to the "Something." and adresses a variable which is logically anded which gives as result if Something.variable has a flag set.
So an example to my last comment and your question(compileable with g++):
// this one fails with a compiler error
// #define ANALYZE1(variable, flag) ((Something.##variable) & (flag))
// this one will address Something.a (struct)
#define ANALYZE2(variable, flag) ((Something.variable) & (flag))
// this one will be Somethinga (global)
#define ANALYZE3(variable, flag) ((Something##variable) & (flag))
#include <iostream>
using namespace std;
struct something{
int a;
};
int Somethinga = 0;
int main()
{
something Something;
Something.a = 1;
if (ANALYZE2(a,1))
cout << "Something.a is 1" << endl;
if (!ANALYZE3(a,1))
cout << "Somethinga is 0" << endl;
return 1;
};
This is not an answer to your question, just a CW post with some tips to help you explore the preprocessor yourself.
The preprocessing step is actually performed prior to any actual code being compiled. In other words, when the compiler starts building your code, no #define statements or anything like that is left.
A good way to understand what the preprocessor does to your code is to get hold of the preprocessed output and look at it.
This is how to do it for Windows:
Create a simple file called test.cpp and put it in a folder, say c:\temp.
Mine looks like this:
#define dog_suffix( variable_name ) variable_name##dog
int main()
{
int dog_suffix( my_int ) = 0;
char dog_suffix( my_char ) = 'a';
return 0;
}
Not very useful, but simple. Open the Visual studio command prompt, navigate to the folder and run the following commandline:
c:\temp>cl test.cpp /P
So, it's the compiler your running (cl.exe), with your file, and the /P option tells the compiler to store the preprocessed output to a file.
Now in the folder next to test.cpp you'll find test.i, which for me looks like this:
#line 1 "test.cpp"
int main()
{
int my_intdog = 0;
char my_chardog = 'a';
return 0;
}
As you can see, no #define left, only the code it expanded into.
According to Wikipedia
Token concatenation, also called token pasting, is one of the most subtle — and easy to abuse — features of the C macro preprocessor. Two arguments can be 'glued' together using ## preprocessor operator; this allows two tokens to be concatenated in the preprocessed code. This can be used to construct elaborate macros which act like a crude version of C++ templates.
Check Token Concatenation
lets consider a different example:
consider
#define MYMACRO(x,y) x##y
without the ##, clearly the preprocessor cant see x and y as separate tokens, can it?
In your example,
#define ANALYZE(variable, flag) ((Something.##variable) & (flag))
## is simply not needed as you are not making any new identifier. In fact, compiler issues "error: pasting "." and "variable" does not give a valid preprocessing token"
I recently learned of the ## functionality that i can define in the beginning of my code. I'm trying to compile the following code:
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <stdio.h>
#include <string>
#define paste(x,y) *x##*y
int main()
{
TCHAR *pcCommPort = "COM";
TCHAR *num = "5";
cout << paste(pcCommPort,num);
return 0;
}
and i keep getting the following error:
expression must have arithmetic or unscoped enum type
it's not liking the fact that i'm using pointers in my "define paste" line. Without any pointers, it'll just return the variable "pcCommPort5." what I want is "COM5."
I've tried _tcscat, strcat, strcat_s, visual studio didn't like any of these....
## doesn't concatenate arbitrary things (especially not strings). What it does it is merges symbols together in the parser into a single symbol.
Let's remove one of those * to see what's going on:
#include <iostream>
#define TO_STRING_HELPER(x) #x
#define TO_STRING(x) TO_STRING_HELPER(x)
#define CONCAT(x, y) *x##y
int main() {
char *pcCommPort = "COM";
char *num = "5";
std::cout << TO_STRING(CONCAT(pcCommPort, num)) << std::endl;
}
Output:
*pcCommPortnum
What CONCAT does in this code is:
Expand x into pcCommPort and y into num. This gives the expression *pcCommPort##num.
Concatenate the two symbols pcCommPort and num into one new symbol: pcCommPortnum. Now the expression is *pcCommPortnum (remember, that last part (pcCommPortnum) is all one symbol).
Finish evaluating the full macro as a * followed by the symbol pcCommPortnum. This becomes the expression *pcCommPortnum. Remember, those are two different symbols: * and pcCommPortnum. The two symbols just follow one after the other.
If we were to try to use *x##*y, what the compiler does is this:
Expand x into pcCommPort and y into num. This gives us the expression *pcCommPort##*num.
Concatenate the two symbols pcCommPort and * into one new symbol: pcCommPort*.
Here, the preprocessor hits an error: the single symbol pcCommPort* is not a valid preprocessing token. Remember, it's not two separate symbols at this point (it is not two symbols pcCommPort followed by *). It is one single symbol (which we call a token).
If you want to concatenate two strings, you're way better off using std::string. You can't* do what you're trying to do with the preprocessor.
*Note, though, that consecutive string literals will be merged together by the compiler (i.e. "COM" "5" will be merged into a single string "COM5" by the compiler). But this only works with string literals, so you'd have to #define pcCommPort "COM" and #define num "5", at which point you could do pcCommPort num (without any further macros) and the compiler would evaluate it to the string "COM5". But unless you really know what you're doing, you really should just use std::string.
I came across a function today in a C++ file that included a #define macro.
The function ends with a return statement, which is followed by a #undef macro.
An example would be...
int test() {
#define STUFF()...
return 0;
#undef STUFF
}
Is this good or bad practice, and is there any advantage or disadvantage to doing this, or does the #undef need to be located before the return to be acknowledged?
Doing that is just fine, besides, the macro name may clash with another macro name. Hence you may use a ridiculous short name like 'A' or ridiculous long name like 'LIBRARY_LIBRARY_NAME_TEMPORARY_MACRO'
You should place the #undef right after the usage of the maco.
The #define and #undef are compiler directives. They are part of the preprocessing of the code and are ran independent of where they are put in relation of the "real" code.
Basically, before the compiler run your code it goes through every directive (#include, #define, #ifdef) and preprocesses your code, so, for example, when you do:
int firstVariable = MY_DIRECTIVE;
#define MY_DIRECTIVE 7
int doesNothing = 9;
int myVariable = MY_DIRECTIVE;
#undef MY_DIRECTIVE
int thirdVariable = MY_DIRECTIVE;
it transforms it in:
int firstVariable = MY_DIRECTIVE;
int doesNothing = 9;
int myVariable = 7;
int thirdVariable = MY_DIRECTIVE;
And only after it will compile your code (in this case giving you a error that MY_DIRECTIVE hadn't been defined on line 1 and 7)
For more info:
Preprocessor directives (cplusplus)
I would not call putting the content of a function by #defined akin to 'best practise'. As #IanMedeiros has indicated it is a pre-compiler directive which means when the compiler runs it has already been expanded. Its difficult to understand what the original author was thinking, but I would would consider a clearer approach which documents more of the authors reasoning with something like:
int test() {
#if defined(INCLUDE_STUFF)
Stuff()
#endif
)