All Questions
2,690
questions
306
votes
6
answers
24k
views
Strange definitions of TRUE and FALSE macros
I have seen the following macro definitions in a coding book.
#define TRUE '/'/'/'
#define FALSE '-'-'-'
There was no explanation there.
Please explain to me how these will work as TRUE and FALSE.
195
votes
5
answers
157k
views
What does "#pragma comment" mean?
What does #pragma comment mean in the following?
#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
176
votes
70
answers
132k
views
What is the worst real-world macros/pre-processor abuse you've ever come across?
What is the worst real-world macros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)?
Please add a short snippet or story if it is really entertaining. The goal ...
161
votes
2
answers
54k
views
Is #pragma once part of the C++11 standard?
Traditionally, the standard and portable way to avoid multiple header inclusions in C++ was/is to use the #ifndef - #define - #endifpre-compiler directives scheme also called macro-guard scheme (see ...
155
votes
6
answers
89k
views
What does the Q_OBJECT macro do? Why do all Qt objects need this macro?
I just started using Qt and noticed that all the example class definitions have the macro Q_OBJECT as the first line. What is the purpose of this preprocessor macro?
141
votes
3
answers
74k
views
When should I prefer constexpr variables over macros?
Where should I prefer using macros and where should I prefer constexpr?
Aren't they basically the same?
#define MAX_HEIGHT 720
vs
constexpr unsigned int max_height = 720;
140
votes
16
answers
138k
views
Optional Parameters with C++ Macros
Is there some way of getting optional parameters with C++ Macros? Some sort of overloading would be nice too.
139
votes
16
answers
130k
views
Is there a __CLASS__ macro in C++?
Is there a __CLASS__ macro in C++ which gives the class name similar to __FUNCTION__ macro which gives the function name
137
votes
14
answers
91k
views
Inline functions vs Preprocessor macros
How does an inline function differ from a preprocessor macro?
131
votes
8
answers
59k
views
Comma in C/C++ macro
Say we have a macro like this
#define FOO(type,name) type name
Which we could use like
FOO(int, int_var);
But not always as simply as that:
FOO(std::map<int, int>, map_var); // error: ...
129
votes
4
answers
129k
views
How to identify platform/compiler from preprocessor macros?
I'm writing a cross-platform code, which should compile at linux, windows, Mac OS. On windows, I must support visual studio and mingw.
There are some pieces of platform-specific code, which I should ...
106
votes
9
answers
83k
views
How can I define a string literal on the GCC command line?
On the GCC command line, I want to define a string such as -Dname=Mary. Then in the source code, I want printf("%s", name); to print Mary.
How could I do it?
82
votes
2
answers
7k
views
What's the point of a PROTOTYPE macro that merely expands to its arguments?
I have a header file which contains
#define PROTOTYPE(s) s
What is the point of that? Seems like it would just replace the input with itself.
There are TONS of other directives around it, but the ...
81
votes
6
answers
68k
views
Can we have recursive macros?
I want to know if we can have recursive macros in C/C++? If yes, please provide a sample example.
Second thing: why am I not able to execute the below code? What is the mistake I am doing? Is it ...
80
votes
5
answers
48k
views
Any utility to test expand C/C++ #define macros?
It seems I often spend way too much time trying to get a #define macro to do exactly what i want. I'll post my current dilemma below and any help is appreciated. But really the bigger question is ...
73
votes
5
answers
11k
views
The use of double include guards in C++
So I recently had a discussion where I work, in which I was questioning the use of a double include guard over a single guard. What I mean by double guard is as follows:
Header file, "header_a.hpp":
...
69
votes
25
answers
30k
views
Are C++ Templates just Macros in disguise?
I've been programming in C++ for a few years, and I've used STL quite a bit and have created my own template classes a few times to see how it's done.
Now I'm trying to integrate templates deeper ...
62
votes
3
answers
27k
views
How do I check for C++20 support? What is the value of __cplusplus for C++20? [duplicate]
Related to questions How do I check for C++11 support? and What is the value of __cplusplus for C++17?
How can I inquire whether the compiler can handle / is set up to use C++20? I know that it is, ...
62
votes
5
answers
91k
views
Which Cross Platform Preprocessor Defines? (__WIN32__ or __WIN32 or WIN32 )?
I often see __WIN32, WIN32 or __WIN32__. I assume that this depends on the used preprocessor (either one from visual studio, or gcc etc).
Do I now have to check first for os and then for the used ...
61
votes
6
answers
42k
views
Which macro to wrap Mac OS X specific code in C/C++
While reading various C and C++ sources, I have encountered two macros __APPLE__ and __OSX__. I found plenty of use of __OSX__ in various codes, especially those originating from *BSD systems.
...
59
votes
5
answers
84k
views
Retrieving a c++ class name programmatically
I was wondering if it is possible in C++ to retrieve the name of a class in string form without having to hardcode it into a variable or a getter. I'm aware that none of that information is actually ...
59
votes
6
answers
49k
views
What is the value of __cplusplus for C++17?
We are trying to test some code under C++17 and its change to std::uncaught_exception. I can't seem to get GCC to provide the value of __cplusplus:
$ /opt/local/bin/g++ -std=c++17 -dM -E - </dev/...
56
votes
4
answers
10k
views
What is double evaluation and why should it be avoided?
I was reading that in C++ using macros like
#define max(a,b) (a > b ? a : b)
can result in a 'double evaluation'. Can someone give me an example of when a double evaluation occurs and why it's ...
52
votes
4
answers
68k
views
Is there a way to get function name inside a C++ function?
I want to implement a function tracer, which would trace how much time a function is taking to execute. I have following class for the same:-
class FuncTracer
{
public:
FuncTracer(LPCTSTR ...
51
votes
8
answers
48k
views
How to generate a newline in a cpp macro?
How do I write a cpp macro which expands to include newlines?
48
votes
4
answers
46k
views
Empty function macros
If I define a function macro with no actual body, is it like an empty string with the compiler (i.e. It doesn't generate any extra instructions at compile time)?
Example:
#define SomeMacro(a)
...
48
votes
5
answers
143k
views
Error: macro names must be identifiers using #ifdef 0
I have the source code of an application written in C++ and I just want to comment something using:
#ifdef 0
...
#endif
And I get this error
error: macro names must be identifiers
Why is this ...
47
votes
8
answers
45k
views
Escaping a # symbol in a #define macro?
Without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.)
For example,...
47
votes
3
answers
15k
views
Can I redefine a C++ macro then define it back?
I am using both the JUCE Library and a number of Boost headers in my code. Juce defines "T" as a macro (groan), and Boost often uses "T" in it's template definitions. The result is that if you ...
47
votes
5
answers
58k
views
Portable UNUSED parameter macro used on function signature for C and C++
I'm interested in creating a macro for eliminating the unused variable warning.
This question describes a way to suppress the unused parameter warning by writing a macro inside the function code:
...
46
votes
21
answers
14k
views
C/C++: How to use the do-while(0); construct without compiler warnings like C4127?
I'm often using do-while(0) constructs in my #defines, for the reasons described in this answer. Also, I'm trying to set the compiler's warning level as high as possible to catch more potential ...
43
votes
5
answers
5k
views
How to call std::min() when min has been defined as a macro?
How do I call std::min when min has already been defined as a macro?
43
votes
7
answers
22k
views
do while(false) pattern [duplicate]
Possible Duplicate:
Why use apparently meaningless do-while and if-else statements in macros?
Why is the do while(false) necessary in the macros below?
#define LOG(message, ...) \
do { \
Lock<...
43
votes
9
answers
56k
views
macro definition containing #include directive
Is there a way to define a macro that contains a #include
directive in its body?
If I just put
the "#include", it gives the error
C2162: "expected macro formal parameter"
since here I am not using #...
43
votes
6
answers
21k
views
Python-like C++ decorators
Are there ways to decorate functions or methods in C++ like in python style?
@decorator
def decorated(self, *args, **kwargs):
pass
Using macros for example:
DECORATE(decorator_method)
int ...
42
votes
5
answers
42k
views
Is it possible to place a macro in a namespace in c++?
My application uses another output than the standard output for logging information, which is why I wrote my own Log(), Error(), Panic() and Assert() functions. To organize things nicely, I enclose ...
39
votes
5
answers
93k
views
How do you implement header guards, and what can you put between them?
At LearnCpp.com | 1.10 — A first look at the preprocessor. Under Header guards, there are those code snippets:
add.h:
#include "mymath.h"
int add(int x, int y);
subtract.h:
#include "...
39
votes
1
answer
18k
views
How does Q_FOREACH (= foreach) macro work and why is it that complex?
In Qt, there is a foreach loop which is implemented using macros (Q_FOREACH). There are different implementations, depending on the compiler.
The definition for GCC is as follows:
#define Q_FOREACH(...
39
votes
3
answers
13k
views
When do we need #ifdef before #undef?
In many of the C and C++ files I have seen macros like this:
#ifdef X
#undef X // no statements in between
#endif
I feel that, it's adequate to simply write:
#undef X
If the macro X wasn't ...
38
votes
5
answers
18k
views
Can I comment multi-line macros?
I'm writing a few large macros and thought it'd be nice if I could add comments to them, like this:
#define SOME_BIG_MACRO(input)\
SOME_FUNCTION_CALL() \ // this does...
SOME_OTHER_FUNCTION_CALL()...
36
votes
4
answers
31k
views
What do __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS mean?
I see this in the standard C++ libraries for my system, as well as some of the headers in a library I'm using.
What are the semantics of these two definitions? Is there a good reference for #defines ...
35
votes
9
answers
27k
views
How do I implement no-op macro (or template) in C++?
How do I implement no-op macro in C++?
#include <iostream>
#ifdef NOOP
#define conditional_noop(x) what goes here?
#else
#define conditional_noop(x) std::cout <&...
35
votes
8
answers
17k
views
how to find the source of some macros
There are many places for defining a macro.When the macro is defined in our own project by us,the are easy to find the definition position for them.
But when i try to learn some famous open source ...
34
votes
3
answers
28k
views
Does Q_UNUSED have any side effects?
Given the following piece of code:
void test(int var)
{
Q_UNUSED(var);
#ifdef SOMETHING
printf("%d",var);
//do something else with var...
#endif
}
Would the Q_UNUSED macro have any ...
33
votes
7
answers
21k
views
C++ - enum vs. const vs. #define
At the end of the article here: http://www.learncpp.com/cpp-tutorial/45-enumerated-types/, it mentions the following:
Finally, as with constant variables, enumerated types show up in the debugger, ...
32
votes
4
answers
32k
views
when to use JNIEXPORT and JNICALL in Android NDK?
I'm trying to write my own jni sources. Looking at some ndk samples, I found that they often use those macros JNIEXPORT and JNICALL follewed by the name of java package like this
JNIEXPORT void ...
32
votes
2
answers
20k
views
Expand macro inside string literal
What I'm trying to do is to #define a macro:
#define a(2)
and later use it inside a string literal: string = "a";.
I want that string to be interpreted not as string but to get the value of a, i.e. ...
32
votes
4
answers
10k
views
Why is this C or C++ macro not expanded by the preprocessor?
Can someone points me the problem in the code when compiled with gcc 4.1.0.
#define X 10
int main()
{
double a = 1e-X;
return 0;
}
I am getting error:Exponent has no digits.
When I replace X ...
31
votes
2
answers
73k
views
Redefining or changing macro value
I am currently working on an already developed project written in MFC C++ and am facing a problem with an already present macro having the definition:
#define HEIGHT_TESTS 13
I am trying to change ...
31
votes
3
answers
14k
views
Are empty macro arguments legal in C++11?
I sometimes deliberately omit macro arguments. For example, for a function-like macro like
#define MY_MACRO(A, B, C) ...
I might call it as:
MY_MACRO(, bar, baz)
There are still technically 3 ...