Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 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