How to control #define inside a condition loop in C++?

X

Xiaoxiao

Hello:

I need to get something like only #define something when a condition
is true, but I can't get it.

For example, if a variable a is set, then I will #define something;
and later on I will only output something when something is #defined.

int a=1; // 1 is just an example

if (a)
#define DBG
.......

#ifdef DBG
fprintf(stderr, "DBG is defined!\n");
#endif

The reason I need to do this is because I had the later part of code
already and don't want to change it because there are lots of them in
the code,
(#ifdef DBG
fprintf(stderr, "DBG is defined!\n");
#endif),
but now I only want to let this part of code be run when a variable
(say a here is set). Or actually, I need to do something like:

#if defined(DBG) || a
then do something
#endif

and the problem is that my a is a variable but not a constant, say if
it is from class A, I can get it like A::a.

I tried
#if defined(DBG) || A::a
then do something
#endif

but I got errors. So I am thinking to only #define DBG when A::a is
set, but it seems the below code will not work as I like:
if (A::a)
{
#define DBG
}

It always do the #define DBG even if the condition of if(A::a) is not
met.

Any help will be appreciated, and sorry for anything unclear in this
post in advance.

Xiaoxiao
 
Ö

Öö Tiib

Hello:

I need to get something like only #define something when a condition
is true, but I can't get it.

For example, if a variable a is set, then I will #define something;
and later on I will only output something when something is #defined.

int a=1; // 1 is just an example

if (a)
#define DBG

Lines starting with # are preprocessor directives. These are separate
language that processes your code before compiling it. Do not use them
in a way like they should care what rest of your code does.
 
X

Xiaoxiao

Lines starting with # are preprocessor directives. These are separate
language that processes your code before compiling it. Do not use them
in a way like they should care what rest of your code does.

Thank you Tiib. Can you suggest what I should do to achieve my purpose
mentioned above?

Thanks again.
 
A

AnonMail2005

Thank you Tiib. Can you suggest what I should do to achieve my purpose
mentioned above?

Thanks again.

Keep the preprocesser directive since it looks like you only want this
to happen with debug builds.

But inside your #if/ #endif pair you can put a valid C++ if statement
that can use your variable.

HTH
 
Ö

Öö Tiib

Thank you Tiib. Can you suggest what I should do to achieve my purpose
mentioned above?

The condition must be true for preprocessor. Preprocessor deals with
macros. Macros you can #define in code or with compilators command
line options. Read docs with your compilator or find a online tutorial
about C++ preprocessor for details.
 
X

Xiaoxiao

Keep the preprocesser directive since it looks like you only want this
to happen with debug builds.

But inside your #if/ #endif pair you can put a valid C++ if statement
that can use your variable.

HTH

Thank you, HTH.

The problem is that I need my variable's condition to meet first and
then to check the previous #defined code, instead of #defined first,
and then my variable determined conditions. Because I will turn off
all the previous #define part of code with only a few of them left
controlled by the new variable.

like:
#define DBG

#ifdef DBG
//dosomething1
#endif

#ifdef DBG
//dosomething2
#endif

now I would like to commented out the //#define DBG, but I would still
leave dosomething2 controlled by my variable. Like,
#if (defined(DBG) || defined(NEW_DBG))
//dosomething2
#endif

NEW_DBG will be defined if my new variable is set. This way, I can
still commented out the #define DBG line, but can have my dosomething2
work for the new variable. By the way, if the #define DBG is listed,
then I still want my dosomething1 and dosomething2 back.

Any more help would be greatly appreciated.
 
V

Vaclav Haisman

Xiaoxiao wrote, On 8.6.2010 21:50:
Hello:

I need to get something like only #define something when a condition
is true, but I can't get it.
Yup, that is not possible.
For example, if a variable a is set, then I will #define something;
and later on I will only output something when something is #defined.

int a=1; // 1 is just an example

if (a)
#define DBG
......
Not possible.
#ifdef DBG
fprintf(stderr, "DBG is defined!\n");
#endif

The reason I need to do this is because I had the later part of code
already and don't want to change it because there are lots of them in
the code,
Well, learn the lesson. Do better planning next time. Bite the bullet and fix
the code.
(#ifdef DBG
fprintf(stderr, "DBG is defined!\n");
#endif),
but now I only want to let this part of code be run when a variable
(say a here is set). Or actually, I need to do something like:

#if defined(DBG) || a
then do something
#endif

and the problem is that my a is a variable but not a constant, say if
it is from class A, I can get it like A::a.

I tried
#if defined(DBG) || A::a
then do something
#endif

but I got errors. So I am thinking to only #define DBG when A::a is
set, but it seems the below code will not work as I like:
if (A::a)
{
#define DBG
}

It always do the #define DBG even if the condition of if(A::a) is not
met.

Any help will be appreciated, and sorry for anything unclear in this
post in advance.
It is simply not possible to have preprocessor definitions depend on run time
variables. What you can use is defining a preprocessor symbol that either
expands into code that checks your variable 'a' or that expands into nothing.
E.g. something like this:

#if defined (DBG)
#define LOG(x) do { if (a) fputs (stderr, x); } while (0)
#else
#define LOG(x) /*empty*/
#endif

This is using macros. You will have to give up printf() like formatting if
you want to use macros. If you insist on using printf() like formatting then
you will have to use real function with ellipsis which is either defined as
doing nothing or as doing the printing using vfprintf(), e.g.:

#include <cstdarg>
#include <cstdio>

void LOG (char const * fmt, ...)
{
#if defined (DBG)
if (a)
{
std::va_list args;
va_start (args, fmt);
std::vfprintf (stderr, fmt, args);
va_end (args);
}
#else
}

Alternatively, instead of reinventing the wheel, you could use one of the
existing C++ logging libraries.
 
X

Xiaoxiao

Xiaoxiao wrote, On 8.6.2010 21:50:> Hello:


Yup, that is not possible.






Not possible.





Well, learn the lesson. Do better planning next time. Bite the bullet and fix
the code.










It is simply not possible to have preprocessor definitions depend on run time
variables. What you can use is defining a preprocessor symbol that either
expands into code that checks your variable 'a' or that expands into nothing.
E.g. something like this:

#if defined (DBG)
#define LOG(x) do { if (a) fputs (stderr, x); } while (0)
#else
#define LOG(x) /*empty*/
#endif

This is using macros. You will have to give up printf() like formatting if
you want to use macros. If you insist on using printf() like formatting then
you will have to use real function with ellipsis which is either defined as
doing nothing or as doing the printing using vfprintf(), e.g.:

#include <cstdarg>
#include <cstdio>

void LOG (char const * fmt, ...)
{
#if defined (DBG)
  if (a)
  {
    std::va_list args;
    va_start (args, fmt);
    std::vfprintf (stderr, fmt, args);
    va_end (args);
  }
#else

}

Alternatively, instead of reinventing the wheel, you could use one of the
existing C++ logging libraries.

Thank you very much for the help, VH, and thanks all again too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top