Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do you nest one #define inside another #define?

Posted on 2003-11-13
5
Medium Priority
?
1,214 Views
Last Modified: 2010-05-18
More specifically how would one include a #ifdef-#endif in a macro so that it will be evaluated not where the macro is defined, but where the macro is invoked.

#define macro #ifdef CONSTANT dosomething() #endif

macro; //this will do nothing
#define CONSTANT
macro; //this will do something

Thanks,
-Sandra
0
Comment
Question by:Sandra-24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9745686
This is impossible, but there is another way which gives the same result:

#ifdef CONSTANT
#define macro DoSomething();
#else
#define macro
#endif

Using:

macro

If CONSTANT is defined, preprocessor replaces this line to

DoSomething();

If CONSTANT is undefined, preproceccor replaces this line with empty line.
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
ID: 9745702
For example, take a look at MFC _T macro. If _UNICODE is defined, _T(x) is defined as L ## x. Without _UNICODE it is defined as x:

#ifdef _UNICODE
#define T(x) L ## x
#else
#define T(x) x
#endif

Result is that line:

CString s = _T("string");

is compiled in UNICODE configuration to:

CString s = L"string";

and in non-UNICODE configuration to:

CString s = "string";

0
 
LVL 3

Expert Comment

by:freewell
ID: 9745730
I guess what Sandra wants to do is turn the marco ON and OFF in multiple places in the file.

such as,

void main()
{
#undef CONSTANT
macro; //this will do nothing

#define CONSTANT
macro; //this will do something
}
0
 
LVL 3

Accepted Solution

by:
norsethomas earned 800 total points
ID: 9745871

Well, this is more an esotherical question :-)

Sandra could also do so by creating a file HH

------HH
#ifdef CONSTANT
#undef macro
#define macro dosomething()
#else
#undef macro
#define macro
#endif
----------

and


main()
{
#define CONSTANT 1
#include "HH"
macro;

...
macro;
macro;
// all macros expanded to dosomething()



#undef  CONSTANT
#include "HH"
macro;
...
...
macro;

// all macros expanded to empty

}


This works, because it's allowed to include a header file more
than once ... If it is a good solution depends on how Sandra
wants to use the macro ...


Thomas



0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9749742
Thanks Thomas, that's a neat workaround. I didn't think of using an include file which is sortof like a macro to begin with. You can include it in many places in your code and you can add all the #ifdefs you want to it:)

-Sandra

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question