Solved

#define, #include inside a MACRO

Posted on 1998-11-18
6
1,071 Views
Last Modified: 2013-11-20
Is there a way to put a #define or a #include inside a MACRO ?

e.g.

#define MY_MACRO(a) \
  #define MY_CONSTANT_##a

#define MY_MACRO2(a) \
  #include #a

I know that these macros can't compile but I would like a way to make such macros.
0
Comment
Question by:jhendrix
  • 3
  • 2
6 Comments
 

Author Comment

by:jhendrix
Comment Utility
Adjusted points to 50
0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
No, you can't.

See Chapter 16 of the C++ FIDS.

The closest you can come is the use of the #include directive with a macro instead of a string name.

#define MYFILE "foo.h"
#include MYFILE

You can generate a macro for the include directive, too:

#define str(s) # s
#define xstr(s) str(s)
#define INCFILE(n) vers ## n
#include xstr(INCFILE(2).h)

B ekiM

0
 

Author Comment

by:jhendrix
Comment Utility

I guess you've actually answered the question ("No I can't") but I really need a way to do this.
As for generating a macro for the include directive, that doesn't help me at all and is pretty basic macro stuff.

Thanx anyways.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Accepted Solution

by:
payn earned 50 total points
Comment Utility
As much as you may want to do this, it's not possible in C++. Partly this is because definitions like this lead to ambiguity, and partly because it would break the one-pass preprocessor into a recursive preprocessor.

That doesn't mean you can't do it, it just means you can't do it without going beyond the basic set of C++ tools, using either a meta-object protocol, a recursive preprocessor, or a simple extra preprocessor pass.

I'd still suggest trying to find a way to restate what you're trying to do. Not every design decision in C/C++ is a good one, but sometimes you can't do something because you really shouldn't need to. Unless you're trying to build a general-purpose library that will be used for purposes beyond your imagining, I'm not sure I see why you're trying to do what you are. However, you might want to look at what you get with #defining const definitions (and maybe templates) and whether that's close enough to replace #defining #defines. For the #include, I guess the closest you can come is conditional compilation around existing #includes, which isn't nearly as general as what you're looking for, but it still serves in many cases.

But, assuming that you absolutely need to do this, let's look at the options:

A meta-object protocol gives you full meta-language capabilities, far beyond a preprocessor, in an object-oriented way. The only usable MOP I know of for Visual C++ is OpenC++ (http://www.softlab.is.tsukuba.ac.jp/~chiba/openc++.html), but it's a great solution. It'll give you all of the functionality you want and more, although in a different way, and with a bit of a learning curve with this.

You could use a recursive preprocessor. I've seen a research project that implemented one, but you'd have to find it (I don't remember where it was) and port it from linux/gcc to Windows/VC.

A simpler way of doing the same thing is to write a script that just runs the standard preprocessor on your code repeatedly until nothing changes. However, be aware that the results may not be what you intuitively expect.

A final possibility is to use a simple pre-preprocessor step. You could just do two steps through the normal preprocessor, but in order to get what you want it may be better to explicitly separate our first-step macros from second-step macros.

Probably the easiest way to do this is to take gcc and modify it to use #macro instead of #define. Then use #macro for your meta-macros and other first-step macros, and #define for your second-step macros. Any #include steps inside first-step macros will not be processed until the second step (after being macro-expanded, which is what you want), while those which are not inside macros will be processed during the first step (so they'll get the benefit of your first-step macro processing).

So for each file, macro-preprocess the file (using gcc -i with your modified gcc) and then pass the results off to the VC compiler as ordinary C++ (or C) source. I'm not sure if you can use a custom build step to do this--you may have to modify the DSP file (generally a bad idea), use a makefile instead, or just run that separate step manually.

Then, for your code, instead of

#define MY_MACRO(a) \
  #define MY_CONSTANT_##a

#define MY_MACRO2(a) \
  #include #a

You'd use

#macro MY_MACRO(a) \
  #define MY_CONSTANT##(a)

#macro MY_MACRO2(a) \
  #include #a

0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
Good luck with your project.

B ekiM

0
 

Author Comment

by:jhendrix
Comment Utility
Thanx PAYN,

I appreciate the time you took to answer my question.
I won't be doing all that stuff though, cause the goal is not worth the effort.
But thanx again

jhendrix
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

763 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

9 Experts available now in Live!

Get 1:1 Help Now