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

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?

Related

preprocessor define with #pragma directive [duplicate]

This question already has answers here:
Pragma in define macro
(4 answers)
Closed 4 months ago.
I would like to do a #define which contains a #pragma directive but I got the following error.
Any idea?
#define FunctionPar_Begin typedef struct fpar { #pragma pack(4)
error C2121: '#': invalid character: possibly the result of a macro expansion
The one you probably want:
#define FunctionPar_Begin typedef struct fpar { _Pragma("pack(4)")
MSVC also allows for:
#define FunctionPar_Begin typedef struct fpar { __pragma(pack(4))
Clang/gcc also allow:
#define FunctionPar_Begin typedef struct fpar { __attribute__((packed, aligned(4)))

|| (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

C- Preprocessor defining a macro [duplicate]

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.