Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

#define, #include inside a MACRO

Posted on 1998-11-18
6
Medium Priority
?
1,240 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Accepted Solution

by:
payn earned 150 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

688 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