C- Preprocessor defining a macro [duplicate] - c++

This question already has answers here:
Preprocessor #if directive
(3 answers)
Closed 8 years ago.
Hello I need a help in defining a macro dependent on a variable.
For Eg:
#if TEMP
#define COUNT 5
#else
#define COUNT 6
#endif
TEMP will be set in a function.
Eg:
void func()
{
TEMP = 1;
}
Setting the count macro and defining temp are in different files.
Can anyone help me on this?
TIA
Sowmya

This is impossible. TEMP will only be set on runtime and is not available for the preprocessing. You could use the -DTEMP as a compiler flag to define it if you need COUNT to be 5. You'd have to change your code into the following:
#ifdef TEMP
#define COUNT 5
#else
#define COUNT 6
#endif

This is not possible. Macros are resolved during compilation, so a runtime change like setting a variable cannot impact them.

Related

_ADDRESSOF(v) macro definition in VS2019 C++ [duplicate]

This question already has answers here:
Implementation of addressof
(4 answers)
Why and when is cast to char volatile& needed?
(2 answers)
Closed last year.
In <vadefs.h> you'll find the following definition for the macro _ADDRESSOF(v) in VS2019:
#ifdef __cplusplus
#define _ADDRESSOF(v) (&const_cast<char&>(reinterpret_cast<const volatile char&>(v)))
#else
#define _ADDRESSOF(v) (&(v))
#endif
I'd like to understand the first definition above, when __cplusplus is defined. Why does it work?

|| (or) equivalent in C/++ preprocessor directive? [duplicate]

This question already has answers here:
how to use #ifdef with an OR condition?
(2 answers)
Closed 2 years ago.
I want to use or in a preprocessor #ifdef directive. I've tried using the || operator but it hasn't worked. How can I do this?
#if defined(SOMETHIG1) || defined(SOMETHING2)
/* .... */
#endif
You can't.
#ifdef COND is short for #if defined(COND). It has no way to combine conditions.
But, you don't need to use #ifdef! If you write it out in full, you can make use of all the operators you need:
#if defined(COND1) || defined(COND2)

What is the difference between #ifdef VALUE vs #if defined (VALUE) [duplicate]

This question already has answers here:
Is #if defined MACRO equivalent to #ifdef MACRO?
(5 answers)
Closed 4 years ago.
#include<stdio.h>
#define MAX 0
int main()
{
#ifdef MAX
printf("MAX defined");
#endif
#if defined (MAX)
printf("MAX is defined");
#endif
return 0;
}
Both the #ifdef and #if defined give the same effect then what is the difference between them? I have not seen the disassembly code of these directives if you have seen then kindly try to explain that as well.
The difference is historical. Originally there was only #ifdef. The newer syntax is more flexible and allows combining tests with logical conditions, but in the simple form you can use them interchangeably.

What does this code mean? "#define TO_LITERAL_(text) #text" [duplicate]

This question already has answers here:
What concept is being exhibited in this macro wrapping?
(1 answer)
Stringification - how does it work?
(2 answers)
Stringify first level macro expansion C
(2 answers)
Macro expansion and stringification: How to get the macro name (not its value) stringified using another macro?
(1 answer)
What is the compiler seeing with this macro? [closed]
(4 answers)
Closed 5 years ago.
When I searched the meaning of __cplusplus, I found a piece of code as following.
#include <stdio.h>
int main() {
#define TO_LITERAL(text) TO_LITERAL_(text)
#define TO_LITERAL_(text) #text
#ifndef __cplusplus
/* this translation unit is being treated as a C one */
printf("a C program\n");
#else
// this translation unit is being treated as a C++ one
printf("a C++ program\n__cplusplus expands to \""
TO_LITERAL(__cplusplus) "\"\n");
#endif
(void)getchar();
return 0;
}
This code gives different output according to which way it's compiled. But I don't know well about the two bold lines.
Why it's wrong if I combine these two lines into one line: #define TO_LITERAL(text) #text
What's the meaning of #text in the second line?
Thank you so much

Macro increase value and then concatenate

I want to create a recursive Macro the will create the "next" class.
Example:
#define PRINTME(indexNum) class m_##(indexNum+1) { }
The indexNum + 1 is evaluated as an int, and won't concatenate to the class name.
How can I cause the compiler to evaluate that, before concatenating?
If you want to generate unique class names every time the PRINTME is invoked then, following is one way:
#define CONCATE1(X,Y) X##Y
#define CONCATE(X,Y) CONCATE1(X,Y)
#define PRINTME class CONCATE(m_,__COUNTER__) {}
__COUNTER__ is an extension in gcc and I am not sure if it's present in other compilers. It's guaranteed that compiler will add 1 every time this macro is invoked.
(In this case, you cannot use __LINE__ or __FILE__ effectively.)
Demo.
The simple answer is that you can't. The preprocessor generally deals in text and tokens; the only place arithmetic is carried out in in #if and #elif directives.
Also, macro expansion isn't recursive. During expansion, the macro being expanded is disabled, and is not available for further substitution.
Well it is doable, based on your motivation and ability to endure ugly code. First off define increment macro:
#define PLUS_ONE(x) PLUS_ONE_##x
#define PLUS_ONE_0 1
#define PLUS_ONE_1 2
#define PLUS_ONE_2 3
#define PLUS_ONE_3 4
#define PLUS_ONE_4 5
#define PLUS_ONE_5 6
#define PLUS_ONE_7 8
#define PLUS_ONE_8 9
#define PLUS_ONE_9 10
// and so on...
You can't just use PLUS_ONE(x) in concatenation operation, since preprocessor won't expand it. There is a way, however - you can abuse the fact that the preprocessor expands variadic arguments.
// pass to variadic macro to expand an argument
#define PRINTME(indexNum) PRINTME_PRIMITIVE(PLUS_ONE(indexNum))
// do concatenation
#define PRINTME_PRIMITIVE(...) class m_ ## __VA_ARGS__ { }
Done!
PRINTME(1); // expands to class m_2 { };
Have you considered using templates instead?