473,383 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Nested macros

Hey,

please consider the following:

#if DEBUG
#define SOMEMACRO(type, name, value) (type) (name) = (value)
#else
#define SOMEMACRO(type, name, value) #define (name) (value)
#endif

If I define DEBUGging, i want the variable (name) to be a variable, but
if not, I want it to be a constant. When I compile this, the preprocessor
starts complaining at the fourth line, because of the second #define. Is
this OT here? And if not, how should I solve this?

the d0ct0r
Jun 27 '08 #1
2 7489
On May 15, 11:21 am, The D0ct0r <thed0c...@xs4all.nlwrote:
Hey,

please consider the following:

#if DEBUG
#define SOMEMACRO(type, name, value) (type) (name) = (value)
#else
#define SOMEMACRO(type, name, value) #define (name) (value)
Why don't you define this macro like this:
#define SOMEMACRO(type, name, value) static const type name = value;

With such constructs, you can construct complex types also, not only
PODs.
#endif

If I define DEBUGging, i want the variable (name) to be a variable, but
if not, I want it to be a constant. When I compile this, the preprocessor
starts complaining at the fourth line, because of the second #define. Is
this OT here? And if not, how should I solve this?

the d0ct0r
Jun 27 '08 #2
On May 15, 8:21 am, The D0ct0r <thed0c...@xs4all.nlwrote:
please consider the following:
#if DEBUG
#define SOMEMACRO(type, name, value) (type) (name) = (value)
#else
#define SOMEMACRO(type, name, value) #define (name) (value)
#endif
If I define DEBUGging, i want the variable (name) to be a
variable, but if not, I want it to be a constant. When I
compile this, the preprocessor starts complaining at the
fourth line, because of the second #define.
The results of macro expansion are not processed as
pre-processing declarations, even if they look like one.
Is this OT here?
No.
And if not, how should I solve this?
You can't use a macro to generate a #define. On the other hand,
you don't want to use #define for constants in C++ anyway.
Something like:

#if DEBUG
#define SOMEMACRO( type, name, value ) type name = value
#else
#define SOMEMACRO( type, name, value ) type const name value
#endif

would probably work. (Note that you cannot put parentheses
around the type. A declaration like:
(int) (toto) = (10) ;
is not legal. You might also want to make the variables
static.)

Also, I very seriously question the wisdom of this. If there's
any reason for the values to not be const, don't make them
const. And if there's not, they should be const in the debug
build as well.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
12
by: Florian | last post by:
Hi, I'm hoping that someone can point me into the right direction here. I'm trying to gain a bit of a better understanding on how the preprocessor works - something that seems like a simple...
7
by: block111 | last post by:
Hello, code like this: int f1(int x){ int f2(int y){ return y*y; } if(x > 0) return f2(x);
5
by: TheKeith | last post by:
I can't figure out why this doesn't work: --------------------------------------------- greeting = new Array(); greeting = "hey"; greeting = "bye"; trace(greeting + greeting);
4
by: rmacias | last post by:
I posted this in another forum, but nobody replied and I need an answer fairly quickly: I'm creating a Windows form application in VC++ .NET 2003. I'm creating the UI is a nested namespace as...
2
by: William Ahern | last post by:
So, I recently learned that Solaris doesn't, or doesn't seem, to provide err.h. On some platforms err.h provides simple wrappers for error printing to stderr. One of which, err(), has this...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
12
by: desktop | last post by:
Are there any performance issues between: bool run = true; while(run) { if (bob1 &&){ //dothing break; }
9
by: V | last post by:
Hello: Consider the following nested for loop: uint64 TABLE; for (i=0; i<=7; i++) for (j=1; j<=7; j++) for (k=1; k<=(1<<j)-1; k++) TABLE = (TABLE) ^ (TABLE);
3
by: Dieter Maurer | last post by:
I met the following surprising behaviour .... for i in range(3): .... def gen1(): .... yield i .... yield i, gen1() .... .... 0 0 1 1
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.