Search results
- Dictionaryuseful/ˈjuːsf(ʊ)l/
adjective
- 1. able to be used for a practical purpose or in several ways: "aspirins are useful for headaches" Similar Opposite
Powered by Oxford Dictionaries
the substituted value need not be legal (or discrete) in the context where the #define is created, as it's evaluated at each point of use, so you can reference not-yet-declared objects, depend on "implementation" that needn't be pre-included, create "constants" such as { 1, 2 } that can be used to initialise arrays, or #define MICROSECONDS *1E ...
Nov 14, 2015 · For some uses of #define macros, this means #define just before a function in .cpp file, then #undef right after the function. The exact use case for #define determines if it should be in .h or in .cpp file. But note that most use cases are actually in violation of 3. above, and you should actually not use #define.
What is #define useful for? Why use constants instead of variables? The answers to which are: #define can do more complicated things then just replace a number with a name. It can actually take parameters. For example: #define getmax(a,b) a>b?a:b does not merely replace a number with a name.
I can't think of a reason why people shouldn't use it, when appropriate. It is useful in some circumstances, and not in others. I think that because it's an interesting technique, some coders perhaps end up using it more often than they should, without real justification. This has given recursion a bad name in some circles.
Nov 27, 2015 · In one of the answers to this question jalf spoke about useful define NOMINMAX, that could prevent from unwanted defining min/max macros. Are there other useful defines that can help to control windows.h (or other Windows headers, for instance Microsoft C Runtime headers or STL implementation) behavior?
Mar 28, 2018 · Most compilers will allow you to define a macro from the command line (e.g. g++ -DDEBUG something.cpp), but you can also just put a define in your code like so: #define DEBUG Some resources: Wikipedia article; C++ specific site; Documentation on GCC's preprocessor; Microsoft reference; C specific site (I don't think it's different from the C++ ...
Sep 3, 2008 · This is useful when you want to pass around a record that has only a couple of fields, or a record that is only used in a few places. In that case specifying a whole new record type with field names (in Python, you'd use an object or a dictionary, as above) could be too verbose.
Nov 27, 2015 · The most common use (by far) of #define is for include guards: // header.hh #ifndef HEADER_HH_ #define HEADER_HH_ namespace pony { // ... } #endif Another common use of #define is in creating a configuration file, commonly a config.h file, where we #define macros based on various states and conditions.
Jun 8, 2011 · A multi-line macro is useful if you have a very complex macro which would be difficult to read if it were all on one line (although it's inadvisable to have very complex macros). In general, you can write a multi-line define using the line-continuation character, \. So e.g.
#ifdef DEBUG #define Debug(x) x #else #define Debug(x) #endif. Which means I can do the following: { Debug(cout << "input == " << input << endl); } and compile to conditionally include the debug statement. I guess I find that useful, but any quality IDE will have plenty of debugging features that overshadow this, right?