This question already has answers here:
Why does clang's stdbool.h contain #define false false
(2 answers)
Closed 6 years ago.
stdbool.h contains this code:
#if __cplusplus < 201103L
/* Defining these macros in C++98 is a GCC extension. */
#define bool bool
#define false false
#define true true
#endif
Why does gcc need to redefine standard C++ types?
Although #define fnord fnord won't generally change the way the identifier fnord is processed, it will cause #ifdef fnord to report the macro as defined. If other code might do something like
#ifndef true
#define true 1
#endif
Having a #define true true would cause such conditional definition to be skipped.
Related
This question already has answers here:
What is the value of an undefined constant used in #if?
(3 answers)
Closed 12 days ago.
May be this is duplicate, I can't find similar question.
My surprise that, following code works for all three big compiler without error
#include <cstdio>
int main() {
#if !_LIBCPP_VERSION
std::printf("_LIBCPP_VERSION not defined");
#else
std::printf("_LIBCPP_VERSION defined and equal to %d", _LIBCPP_VERSION);
#endif
#ifndef _LIBCPP_VERSION
std::printf("_LIBCPP_VERSION not defined");
#else
std::printf("_LIBCPP_VERSION defined and equal to %d", _LIBCPP_VERSION);
#endif
}
Link to godbolt
My question is that: There check #if !_LIBCPP_VERSION - is always similar with #ifndef _LIBCPP_VERSION by standard C or C++?
They are not equivalent for numerical values.
SOME_MACROS
#if !SOME_MACROS
#ifndef SOME_MACROS
x
True
False
1
False
False
0
True
False
undef
True
True
Now, you said you only care about numerical values, so only the middle rows are relevant here. Yet we see a difference in those two rows.
is there a way/trick to make a #define directive evaluate some condition?
for example
#define COM_TIME_DO(COND, BODY) \
#if (COND) BODY
#else
#endif
it's ok also to use template but body must be an arbitrary (correct in the context is used to) piece of code, simply just present or not in the source depending of COND.
as it is now the previous code doesn't even compile.
the goal of this question is primarly a better knowledge of the language and what i'm trying to do is define a debug macro system that i can activate selectively on certain parts of code for example:
A.hpp
#define A_TEST_1 1
#define A_TEST_2 0
Class A {
...
COM_TIME_DO(A_TEST_1,
void test_method_1();
)
COM_TIME_DO(A_TEST_2,
void test_method_2();
)
};
A.cpp
COM_TIME_DO(A_TEST_1,
void A::test_method_1() {
...
})
COM_TIME_DO(A_TEST_2,
void A::test_method_2() {
...
})
i was just asking if it was POSSIBLE because i like it more than the #if ... #endif.
If the expression of the condition will always expand to 1 or 0 (or some other known set of values) it is possible to implement such a macro.
#define VALUE_0(...)
#define VALUE_1(...) __VA_ARGS__
#define COM_TIME_DO_IN(A, ...) VALUE_##A(__VA_ARGS__)
#define COM_TIME_DO(A, ...) COM_TIME_DO_IN(A, __VA_ARGS__)
However, do not use such code in real life. Use #if and write clear, readable and maintainable code that is easy to understand for anyone.
is there a way/trick to make a #define directive evaluate some condition?
This depends on what the condition actually is.
Since you mentioned #if I'm assuming you'd like to evaluate an integer constant expressions.
Doing this in a macro single macro isn't possible, without implementation defined _Pragmas, but you can do it with an include + a macro definition:
#define COM_TIME_DO ((1 > 2), true, false)
#include "com-time-do.h"
// ^-- generates: false
#define COM_TIME_DO ((1 == 1), true_func();, false_func();)
#include "com-time-do.h"
// ^-- generates: true_func();
where com-time-do.h is defined as follows:
// com-time-do.h
#define SCAN(...) __VA_ARGS__
#define SLOT_AT_COND(a,b,c) a
#define SLOT_AT_THEN(a,b,c) b
#define SLOT_AT_ELSE(a,b,c) c
#if SCAN(SLOT_AT_COND COM_TIME_DO)
SCAN(SLOT_AT_THEN COM_TIME_DO)
#else
SCAN(SLOT_AT_ELSE COM_TIME_DO)
#endif
#undef COM_TIME_DO
Although, as KamilCuk said, please write reasonable code and don't use this.
This question already has answers here:
C++, using #if TRUE conditional directive
(4 answers)
Closed 1 year ago.
The following code prints main.cpp:20:19: note: #pragma message: serial disabled. I had expected that "enabled" would be printed. What is wrong with the code?
int main()
{
#define SERIAL_ENABLED TRUE
#if SERIAL_ENABLED
#pragma message "serial enabled"
#else
#pragma message "serial disabled"
#endif
return 0;
}
TRUE is not defined. Use true.
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.
In the source code of stdbool.h in LLVM project, it reads:
/* Don't define bool, true, and false in C++, except as a GNU extension. */
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
/* Define _Bool, bool, false, true as a GNU extension. */
#define _Bool bool
#define bool bool
#define false false
#define true true
#endif
In the last 4 lines there are three lines of the from #define X X. Why would you do that? What difference does it make? Wouldn't this force compiler to just replace, say, true with true?
The only reason I can think of is, that preprocessor statements like
#ifdef bool
// do some stuff or define bool
#endif
in other c files include afterwards will work proper and not trying to redefine bool in another way like
#define bool int
which would interfere with the first definition
#define X X
has the effect that "the pre-processor conditional"*:
#ifdef X
is "true" "succeeds".*
* update
It would make the difference that true, false etc are now macros. So code like this
#if defined(true)
...
#else
...
#endif
would be affected.