Solved

#define, #include inside a MACRO

Posted on 1998-11-18
6
1,208 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
[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
  • 3
  • 2
6 Comments
 

Author Comment

by:jhendrix
ID: 1324932
Adjusted points to 50
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1324933
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
ID: 1324934

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
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 
LVL 1

Accepted Solution

by:
payn earned 50 total points
ID: 1324935
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
ID: 1324936
Good luck with your project.

B ekiM

0
 

Author Comment

by:jhendrix
ID: 1324937
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

626 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