Cascaded Macros -- Back-Up

F

Frank

It would be nice to have short macro names in a header file,
whilest the probability of name clashes increases. Is there a
way how to circumvent the 'normal' C-pre-processor handling
of cascaded macros, such that something like the following
would make sense?

#ifdef SHORTY
# TMP_BACKUP_OF_SHORTY (SHORTY)
#endif

#define SHORT my_nice_expression

....
using SHORTY
....

#ifdef TMP_BACKUP_OF_SHORTY
# define SHORTY TMP_BACKUP_OF_SHORTY
#endif

Best Regards

Frank
 
E

Eric Sosman

Frank said:
It would be nice to have short macro names in a header file,

Why?
whilest the probability of name clashes increases. Is there a
way how to circumvent the 'normal' C-pre-processor handling
of cascaded macros, such that something like the following
would make sense?

#ifdef SHORTY
# TMP_BACKUP_OF_SHORTY (SHORTY)
#endif

#define SHORT my_nice_expression

...
using SHORTY
...

#ifdef TMP_BACKUP_OF_SHORTY
# define SHORTY TMP_BACKUP_OF_SHORTY
#endif

No. Choose good names and stick with them; don't try
to introduce aliases. (Besides, they wouldn't solve the
problem of clashes: What if TMP_BACKUP_OF_SHORTY is already
in use? Don't imagine that nobody would invent such a name;
you're *already* supposing at least two independent inventions
of SHORTY, so why not two inventions of TMP_BACKUP_OF_x as a
"standard pattern" for backup names?)

I've used compilers that could do something like this
using compiler-specific extensions like

#define SHORTY xxx
#pragma push SHORTY
/* now SHORTY is undefined */
#define SHORTY yyy
/* now SHORTY expands to yyy */
#pragma pop SHORTY
/* now SHORTY expands to xxx again */

.... but as soon as you decide to use such a gizmo, your code
is tied to that specific compiler forever and will not work
with any other -- which is why I never used that feature even
on compilers that offered it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top