Solved

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

Posted on 2003-11-13
5
1,129 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
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 50 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 200 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now