Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an appropriate use scenario of #define in C++? [closed]

I know the basic rules, use inline, enum and const instead of #define, that is not what I'm after with this question. What I want to know is what is considered an acceptable scenario in which you would use a #define macro, and how, in C++.

Please do not post question or links to "define vs const" questions or "preprocessor vs compiler", and I've already gone through Effective C++ by Scott Meyers and I know the advantages of one over the other.

However after hours and hours of surfing the net, I get the feeling #define is treated as some kind of underdog in C++, but I'm sure there must be a case in which it could be acceptable, even desirable, to use it.

To get the ball rolling I guess one scenario I could think of is to create a DEBUG macro that based on it enables prints and whatnot all over the code for debug purposes.

like image 746
gorba Avatar asked Feb 25 '14 07:02

gorba


People also ask

What is the use of scenario?

Scenarios are descriptions of one or more users interacting with with a system, device, or process to achieve a goal under specified conditions and constraints. They provide information about the context in which a system has to operate, in a user- and task -oriented way.

What is scenario in communication skills?

A scenario is a description of a series of interactions between a player and a virtual character for one-to-one communication skills training, where at each step the player is faced with a choice between statements.

What are usage scenarios in software engineering?

A usage scenario is a description of a way someone uses an existing product or system. A design scenario is a description of envisioned usage of a product or system being designed.

What is a design scenario?

Design scenarios are text-based stories that form the foundation of a storyboard, which adds sketches to illustrate the text. An individual design scenario need not be exhaustive: function-interaction tables and user guides provide a more systematic overview of the system.


1 Answers

Here are a few scenarios where using #define is a good solution:

Adding diagnostics information while preserving function signature:

#ifdef _DEBUG
#define Log(MSG)  Log((MSG), __FILE__, __LINE__);
#endif

Conditional compilation and include guards are also a good example (no example given, as you should understand this :)).

Boilerplate code is another example, but this can easily be abused. A good example of using macros for boilerplate code is the BOOST_AUTO_TEST_CASE macro in Boost.UnitTest (a worse example is the WinAPI macro set that maps Windows APIs to their CHAR or WCHAR macros).

Another good example is providing compiler-specific keywords and settings:

#if (defined _WIN32) && (defined LIB_SHARED)
#   ifdef LIB_EXPORTS
#       define LIB_EXPORT __declspec(dllexport)
#   else
#       define LIB_EXPORT __declspec(dllimport)
#   endif /* LIB_EXPORTS */
#else
#   define LIB_EXPORT extern
#endif /* _WIN32 && LIB_SHARED */

Usage:

// forward declaration of API function:
LIB_EXPORT void MyFunction(int);
like image 129
utnapistim Avatar answered Oct 10 '22 22:10

utnapistim