Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Apr 23, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using MACRO Definitions

By using MACRO definitions, you can easily write lines of code that are only included in Debug versions and not in Release versions. For instance, if you create a function 'MyLog(x,y)' that writes debugging information to a file you can define a macro such as:
#ifdef DEBUG
#define LOG(a,b)   MyLog(a,b);
#define LOG(a,b)

Using conditional compilation, you can remove the 'MyLog' code from your Release version. However, in case of logging, you want to pass a variable amount of parameters most of the time. In this case the macro definition mentioned above doesn't work. Nevertheless, there is a nice workaround. The solution is to redefine the macro as:
#ifdef DEBUG
#define LOG(a)   MyLog a;
#define LOG(a)

In your code you should use it as:
LOG ((a,b,c))
LOG ((a,b,c,d,e))

By putting the parameters between double brackets, you can pass a variable amount of parameters to your macro.
Dominique Bijnens
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date