Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 416
c++ getting the first 10 characters of a char* string 11 96
thread-safe code in c++ 2 104
How to split this in C++ 4 103
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 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