Solved

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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

624 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