This question already has answers here:
Creating C macro with ## and __LINE__ (token concatenation with positioning macro)
(3 answers)
Why do I need double layer of indirection for macros?
(5 answers)
Closed 5 years ago.
Consider the following code:
struct S {};
#define CREATE_INSTANCE S instance_##__LINE__
int main()
{
CREATE_INSTANCE;
CREATE_INSTANCE;
return 0;
}
What I want it to do is create two instances of S named instance_7 and instance_8. What it actually does is creates instance___LINE__ twice.
How to achieve what I want?
Using some indirection:
#define Concat_(a, b) a ## b
#define Concat(a, b) Concat_(a, b)
#define CREATE_INSTANCE S Concat(instance_, __LINE__)
Related
This question already has answers here:
Creating C macro with ## and __LINE__ (token concatenation with positioning macro)
(3 answers)
Closed 27 days ago.
The ## are not working with an string an a macro. My compiler not extract the value of the macro or something else after ##
#define ProfilerScope(name) Timer timer ## __LINE__;
This just returns me Timer timer__LINE__
I tried to make a STRINGIZE macro to makes this works but nothing, they just returns me the same
#define STRINGIZE(x) STRINGIZE_SIMPLE(x)
#define STRINGIZE_SIMPLE(x) #x
#define ProfilerScope(name) Timer timer ## STRINGIZE(__LINE__);
return Timer timerSTRINGIZE(__LINE__)
I want it to return the line of code where I'm using the macro
I want it to return the line of code where I'm using the macro
I think you want to expand __LINE__ and then concatenate it with a word. You have to expand it in a macro. The following code:
#define CONCAT(a, b) CONCAT2(a, b)
#define CONCAT2(a, b) a##b
#define ProfilerScope(name) Timer CONCAT(timer, __LINE__);
ProfilerScope()
outputs Timer timer4;.
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?
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.
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
This question already has answers here:
What does #x inside a C macro mean?
(4 answers)
Closed 4 years ago.
What does the pound sign indicate in this line of code?
#define CONDITION(x) if(!(x)){ HandleError(#x,__FUNCTION__,__LINE__);return false;}
This is how it is being called:
CONDITION(foo != false);
A single # before a macro parameter converts it to a string literal.
#define STRINGIFY(x) #x
STRINGIFY(hello) // expands to "hello"
In your example, the string would be "foo != false", so that the error message shows the code that was being tested.
A double ## between two tokens within a macro combines them into a single token
#define GLOM(x,y) x ## y
GLOM(hello, World) // expands to helloWorld